xref: /OK3568_Linux_fs/kernel/drivers/thermal/intel/int340x_thermal/int3402_thermal.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * INT3402 thermal driver for memory temperature reporting
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2014, Intel Corporation
6*4882a593Smuzhiyun  * Authors: Aaron Lu <aaron.lu@intel.com>
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #include <linux/module.h>
10*4882a593Smuzhiyun #include <linux/platform_device.h>
11*4882a593Smuzhiyun #include <linux/acpi.h>
12*4882a593Smuzhiyun #include <linux/thermal.h>
13*4882a593Smuzhiyun #include "int340x_thermal_zone.h"
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #define INT3402_PERF_CHANGED_EVENT	0x80
16*4882a593Smuzhiyun #define INT3402_THERMAL_EVENT		0x90
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun struct int3402_thermal_data {
19*4882a593Smuzhiyun 	acpi_handle *handle;
20*4882a593Smuzhiyun 	struct int34x_thermal_zone *int340x_zone;
21*4882a593Smuzhiyun };
22*4882a593Smuzhiyun 
int3402_notify(acpi_handle handle,u32 event,void * data)23*4882a593Smuzhiyun static void int3402_notify(acpi_handle handle, u32 event, void *data)
24*4882a593Smuzhiyun {
25*4882a593Smuzhiyun 	struct int3402_thermal_data *priv = data;
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun 	if (!priv)
28*4882a593Smuzhiyun 		return;
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun 	switch (event) {
31*4882a593Smuzhiyun 	case INT3402_PERF_CHANGED_EVENT:
32*4882a593Smuzhiyun 		break;
33*4882a593Smuzhiyun 	case INT3402_THERMAL_EVENT:
34*4882a593Smuzhiyun 		int340x_thermal_zone_device_update(priv->int340x_zone,
35*4882a593Smuzhiyun 						   THERMAL_TRIP_VIOLATED);
36*4882a593Smuzhiyun 		break;
37*4882a593Smuzhiyun 	default:
38*4882a593Smuzhiyun 		break;
39*4882a593Smuzhiyun 	}
40*4882a593Smuzhiyun }
41*4882a593Smuzhiyun 
int3402_thermal_probe(struct platform_device * pdev)42*4882a593Smuzhiyun static int int3402_thermal_probe(struct platform_device *pdev)
43*4882a593Smuzhiyun {
44*4882a593Smuzhiyun 	struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
45*4882a593Smuzhiyun 	struct int3402_thermal_data *d;
46*4882a593Smuzhiyun 	int ret;
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun 	if (!acpi_has_method(adev->handle, "_TMP"))
49*4882a593Smuzhiyun 		return -ENODEV;
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun 	d = devm_kzalloc(&pdev->dev, sizeof(*d), GFP_KERNEL);
52*4882a593Smuzhiyun 	if (!d)
53*4882a593Smuzhiyun 		return -ENOMEM;
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun 	d->int340x_zone = int340x_thermal_zone_add(adev, NULL);
56*4882a593Smuzhiyun 	if (IS_ERR(d->int340x_zone))
57*4882a593Smuzhiyun 		return PTR_ERR(d->int340x_zone);
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun 	ret = acpi_install_notify_handler(adev->handle,
60*4882a593Smuzhiyun 					  ACPI_DEVICE_NOTIFY,
61*4882a593Smuzhiyun 					  int3402_notify,
62*4882a593Smuzhiyun 					  d);
63*4882a593Smuzhiyun 	if (ret) {
64*4882a593Smuzhiyun 		int340x_thermal_zone_remove(d->int340x_zone);
65*4882a593Smuzhiyun 		return ret;
66*4882a593Smuzhiyun 	}
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun 	d->handle = adev->handle;
69*4882a593Smuzhiyun 	platform_set_drvdata(pdev, d);
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun 	return 0;
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun 
int3402_thermal_remove(struct platform_device * pdev)74*4882a593Smuzhiyun static int int3402_thermal_remove(struct platform_device *pdev)
75*4882a593Smuzhiyun {
76*4882a593Smuzhiyun 	struct int3402_thermal_data *d = platform_get_drvdata(pdev);
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun 	acpi_remove_notify_handler(d->handle,
79*4882a593Smuzhiyun 				   ACPI_DEVICE_NOTIFY, int3402_notify);
80*4882a593Smuzhiyun 	int340x_thermal_zone_remove(d->int340x_zone);
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	return 0;
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun static const struct acpi_device_id int3402_thermal_match[] = {
86*4882a593Smuzhiyun 	{"INT3402", 0},
87*4882a593Smuzhiyun 	{}
88*4882a593Smuzhiyun };
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun MODULE_DEVICE_TABLE(acpi, int3402_thermal_match);
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun static struct platform_driver int3402_thermal_driver = {
93*4882a593Smuzhiyun 	.probe = int3402_thermal_probe,
94*4882a593Smuzhiyun 	.remove = int3402_thermal_remove,
95*4882a593Smuzhiyun 	.driver = {
96*4882a593Smuzhiyun 		   .name = "int3402 thermal",
97*4882a593Smuzhiyun 		   .acpi_match_table = int3402_thermal_match,
98*4882a593Smuzhiyun 		   },
99*4882a593Smuzhiyun };
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun module_platform_driver(int3402_thermal_driver);
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun MODULE_DESCRIPTION("INT3402 Thermal driver");
104*4882a593Smuzhiyun MODULE_LICENSE("GPL");
105