2 * Intel Broxton PMIC thermal driver
4 * Copyright (C) 2016 Intel Corporation. All rights reserved.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version
8 * 2 as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/slab.h>
20 #include <linux/delay.h>
21 #include <linux/interrupt.h>
22 #include <linux/device.h>
23 #include <linux/thermal.h>
24 #include <linux/platform_device.h>
25 #include <linux/sched.h>
26 #include <linux/mfd/intel_soc_pmic.h>
28 #define BXTWC_THRM0IRQ 0x4E04
29 #define BXTWC_THRM1IRQ 0x4E05
30 #define BXTWC_THRM2IRQ 0x4E06
31 #define BXTWC_MTHRM0IRQ 0x4E12
32 #define BXTWC_MTHRM1IRQ 0x4E13
33 #define BXTWC_MTHRM2IRQ 0x4E14
34 #define BXTWC_STHRM0IRQ 0x4F19
35 #define BXTWC_STHRM1IRQ 0x4F1A
36 #define BXTWC_STHRM2IRQ 0x4F1B
38 struct trip_config_map
{
48 struct thermal_irq_map
{
51 const struct trip_config_map
*trip_config
;
54 struct pmic_thermal_data
{
55 const struct thermal_irq_map
*maps
;
59 static const struct trip_config_map bxtwc_str0_trip_config
[] = {
61 .irq_reg
= BXTWC_THRM0IRQ
,
63 .irq_en
= BXTWC_MTHRM0IRQ
,
65 .evt_stat
= BXTWC_STHRM0IRQ
,
70 .irq_reg
= BXTWC_THRM0IRQ
,
72 .irq_en
= BXTWC_MTHRM0IRQ
,
74 .evt_stat
= BXTWC_STHRM0IRQ
,
80 static const struct trip_config_map bxtwc_str1_trip_config
[] = {
82 .irq_reg
= BXTWC_THRM0IRQ
,
84 .irq_en
= BXTWC_MTHRM0IRQ
,
86 .evt_stat
= BXTWC_STHRM0IRQ
,
91 .irq_reg
= BXTWC_THRM0IRQ
,
93 .irq_en
= BXTWC_MTHRM0IRQ
,
95 .evt_stat
= BXTWC_STHRM0IRQ
,
101 static const struct trip_config_map bxtwc_str2_trip_config
[] = {
103 .irq_reg
= BXTWC_THRM0IRQ
,
105 .irq_en
= BXTWC_MTHRM0IRQ
,
107 .evt_stat
= BXTWC_STHRM0IRQ
,
112 .irq_reg
= BXTWC_THRM0IRQ
,
114 .irq_en
= BXTWC_MTHRM0IRQ
,
116 .evt_stat
= BXTWC_STHRM0IRQ
,
122 static const struct trip_config_map bxtwc_str3_trip_config
[] = {
124 .irq_reg
= BXTWC_THRM2IRQ
,
126 .irq_en
= BXTWC_MTHRM2IRQ
,
128 .evt_stat
= BXTWC_STHRM2IRQ
,
134 static const struct thermal_irq_map bxtwc_thermal_irq_map
[] = {
137 .trip_config
= bxtwc_str0_trip_config
,
138 .num_trips
= ARRAY_SIZE(bxtwc_str0_trip_config
),
142 .trip_config
= bxtwc_str1_trip_config
,
143 .num_trips
= ARRAY_SIZE(bxtwc_str1_trip_config
),
147 .trip_config
= bxtwc_str2_trip_config
,
148 .num_trips
= ARRAY_SIZE(bxtwc_str2_trip_config
),
152 .trip_config
= bxtwc_str3_trip_config
,
153 .num_trips
= ARRAY_SIZE(bxtwc_str3_trip_config
),
157 static const struct pmic_thermal_data bxtwc_thermal_data
= {
158 .maps
= bxtwc_thermal_irq_map
,
159 .num_maps
= ARRAY_SIZE(bxtwc_thermal_irq_map
),
162 static irqreturn_t
pmic_thermal_irq_handler(int irq
, void *data
)
164 struct platform_device
*pdev
= data
;
165 struct thermal_zone_device
*tzd
;
166 struct pmic_thermal_data
*td
;
167 struct intel_soc_pmic
*pmic
;
168 struct regmap
*regmap
;
169 u8 reg_val
, mask
, irq_stat
;
170 u16 reg
, evt_stat_reg
;
173 pmic
= dev_get_drvdata(pdev
->dev
.parent
);
174 regmap
= pmic
->regmap
;
175 td
= (struct pmic_thermal_data
*)
176 platform_get_device_id(pdev
)->driver_data
;
178 /* Resolve thermal irqs */
179 for (i
= 0; i
< td
->num_maps
; i
++) {
180 for (j
= 0; j
< td
->maps
[i
].num_trips
; j
++) {
181 reg
= td
->maps
[i
].trip_config
[j
].irq_reg
;
182 mask
= td
->maps
[i
].trip_config
[j
].irq_mask
;
184 * Read the irq register to resolve whether the
185 * interrupt was triggered for this sensor
187 if (regmap_read(regmap
, reg
, &ret
))
191 irq_stat
= ((u8
)ret
& mask
);
197 * Read the status register to find out what
198 * event occurred i.e a high or a low
200 evt_stat_reg
= td
->maps
[i
].trip_config
[j
].evt_stat
;
201 if (regmap_read(regmap
, evt_stat_reg
, &ret
))
204 tzd
= thermal_zone_get_zone_by_name(td
->maps
[i
].handle
);
206 thermal_zone_device_update(tzd
,
207 THERMAL_EVENT_UNSPECIFIED
);
209 /* Clear the appropriate irq */
210 regmap_write(regmap
, reg
, reg_val
& mask
);
217 static int pmic_thermal_probe(struct platform_device
*pdev
)
219 struct regmap_irq_chip_data
*regmap_irq_chip
;
220 struct pmic_thermal_data
*thermal_data
;
221 int ret
, irq
, virq
, i
, j
, pmic_irq_count
;
222 struct intel_soc_pmic
*pmic
;
223 struct regmap
*regmap
;
229 pmic
= dev_get_drvdata(pdev
->dev
.parent
);
231 dev_err(dev
, "Failed to get struct intel_soc_pmic pointer\n");
235 thermal_data
= (struct pmic_thermal_data
*)
236 platform_get_device_id(pdev
)->driver_data
;
238 dev_err(dev
, "No thermal data initialized!!\n");
242 regmap
= pmic
->regmap
;
243 regmap_irq_chip
= pmic
->irq_chip_data
;
246 while ((irq
= platform_get_irq(pdev
, pmic_irq_count
)) != -ENXIO
) {
247 virq
= regmap_irq_get_virq(regmap_irq_chip
, irq
);
249 dev_err(dev
, "failed to get virq by irq %d\n", irq
);
253 ret
= devm_request_threaded_irq(&pdev
->dev
, virq
,
254 NULL
, pmic_thermal_irq_handler
,
255 IRQF_ONESHOT
, "pmic_thermal", pdev
);
258 dev_err(dev
, "request irq(%d) failed: %d\n", virq
, ret
);
264 /* Enable thermal interrupts */
265 for (i
= 0; i
< thermal_data
->num_maps
; i
++) {
266 for (j
= 0; j
< thermal_data
->maps
[i
].num_trips
; j
++) {
267 reg
= thermal_data
->maps
[i
].trip_config
[j
].irq_en
;
268 mask
= thermal_data
->maps
[i
].trip_config
[j
].irq_en_mask
;
269 ret
= regmap_update_bits(regmap
, reg
, mask
, 0x00);
278 static const struct platform_device_id pmic_thermal_id_table
[] = {
280 .name
= "bxt_wcove_thermal",
281 .driver_data
= (kernel_ulong_t
)&bxtwc_thermal_data
,
286 static struct platform_driver pmic_thermal_driver
= {
287 .probe
= pmic_thermal_probe
,
289 .name
= "pmic_thermal",
291 .id_table
= pmic_thermal_id_table
,
294 MODULE_DEVICE_TABLE(platform
, pmic_thermal_id_table
);
295 module_platform_driver(pmic_thermal_driver
);
297 MODULE_AUTHOR("Yegnesh S Iyer <yegnesh.s.iyer@intel.com>");
298 MODULE_DESCRIPTION("Intel Broxton PMIC Thermal Driver");
299 MODULE_LICENSE("GPL v2");