1 /*
2  * intel_soc_dts_thermal.c
3  * Copyright (c) 2014, Intel Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  */
15 
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 
18 #include <linux/acpi.h>
19 #include <linux/module.h>
20 #include <linux/interrupt.h>
21 #include <asm/cpu_device_id.h>
22 #include <asm/intel-family.h>
23 #include "intel_soc_dts_iosf.h"
24 
25 #define CRITICAL_OFFSET_FROM_TJ_MAX	5000
26 
27 static int crit_offset = CRITICAL_OFFSET_FROM_TJ_MAX;
28 module_param(crit_offset, int, 0644);
29 MODULE_PARM_DESC(crit_offset,
30 	"Critical Temperature offset from tj max in millidegree Celsius.");
31 
32 /* IRQ 86 is a fixed APIC interrupt for BYT DTS Aux threshold notifications */
33 #define BYT_SOC_DTS_APIC_IRQ	86
34 
35 static int soc_dts_thres_gsi;
36 static int soc_dts_thres_irq;
37 static struct intel_soc_dts_sensors *soc_dts;
38 
39 static irqreturn_t soc_irq_thread_fn(int irq, void *dev_data)
40 {
41 	pr_debug("proc_thermal_interrupt\n");
42 	intel_soc_dts_iosf_interrupt_handler(soc_dts);
43 
44 	return IRQ_HANDLED;
45 }
46 
47 static const struct x86_cpu_id soc_thermal_ids[] = {
48 	{ X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_SILVERMONT, 0,
49 		BYT_SOC_DTS_APIC_IRQ},
50 	{}
51 };
52 MODULE_DEVICE_TABLE(x86cpu, soc_thermal_ids);
53 
54 static int __init intel_soc_thermal_init(void)
55 {
56 	int err = 0;
57 	const struct x86_cpu_id *match_cpu;
58 
59 	match_cpu = x86_match_cpu(soc_thermal_ids);
60 	if (!match_cpu)
61 		return -ENODEV;
62 
63 	/* Create a zone with 2 trips with marked as read only */
64 	soc_dts = intel_soc_dts_iosf_init(INTEL_SOC_DTS_INTERRUPT_APIC, 2, 1);
65 	if (IS_ERR(soc_dts)) {
66 		err = PTR_ERR(soc_dts);
67 		return err;
68 	}
69 
70 	soc_dts_thres_gsi = (int)match_cpu->driver_data;
71 	if (soc_dts_thres_gsi) {
72 		/*
73 		 * Note the flags here MUST match the firmware defaults, rather
74 		 * then the request_irq flags, otherwise we get an EBUSY error.
75 		 */
76 		soc_dts_thres_irq = acpi_register_gsi(NULL, soc_dts_thres_gsi,
77 						      ACPI_LEVEL_SENSITIVE,
78 						      ACPI_ACTIVE_LOW);
79 		if (soc_dts_thres_irq < 0) {
80 			pr_warn("intel_soc_dts: Could not get IRQ for GSI %d, err %d\n",
81 				soc_dts_thres_gsi, soc_dts_thres_irq);
82 			soc_dts_thres_irq = 0;
83 		}
84 	}
85 
86 	if (soc_dts_thres_irq) {
87 		err = request_threaded_irq(soc_dts_thres_irq, NULL,
88 					   soc_irq_thread_fn,
89 					   IRQF_TRIGGER_RISING | IRQF_ONESHOT,
90 					   "soc_dts", soc_dts);
91 		if (err) {
92 			/*
93 			 * Do not just error out because the user space thermal
94 			 * daemon such as DPTF may use polling instead of being
95 			 * interrupt driven.
96 			 */
97 			pr_warn("request_threaded_irq ret %d\n", err);
98 		}
99 	}
100 
101 	err = intel_soc_dts_iosf_add_read_only_critical_trip(soc_dts,
102 							     crit_offset);
103 	if (err)
104 		goto error_trips;
105 
106 	return 0;
107 
108 error_trips:
109 	if (soc_dts_thres_irq) {
110 		free_irq(soc_dts_thres_irq, soc_dts);
111 		acpi_unregister_gsi(soc_dts_thres_gsi);
112 	}
113 	intel_soc_dts_iosf_exit(soc_dts);
114 
115 	return err;
116 }
117 
118 static void __exit intel_soc_thermal_exit(void)
119 {
120 	if (soc_dts_thres_irq) {
121 		free_irq(soc_dts_thres_irq, soc_dts);
122 		acpi_unregister_gsi(soc_dts_thres_gsi);
123 	}
124 	intel_soc_dts_iosf_exit(soc_dts);
125 }
126 
127 module_init(intel_soc_thermal_init)
128 module_exit(intel_soc_thermal_exit)
129 
130 MODULE_DESCRIPTION("Intel SoC DTS Thermal Driver");
131 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
132 MODULE_LICENSE("GPL v2");
133