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
, trip
;
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 trip
= td
->maps
[i
].trip_config
[j
].trip_num
;
205 tzd
= thermal_zone_get_zone_by_name(td
->maps
[i
].handle
);
207 thermal_zone_device_update(tzd
,
208 THERMAL_EVENT_UNSPECIFIED
);
210 /* Clear the appropriate irq */
211 regmap_write(regmap
, reg
, reg_val
& mask
);
218 static int pmic_thermal_probe(struct platform_device
*pdev
)
220 struct regmap_irq_chip_data
*regmap_irq_chip
;
221 struct pmic_thermal_data
*thermal_data
;
222 int ret
, irq
, virq
, i
, j
, pmic_irq_count
;
223 struct intel_soc_pmic
*pmic
;
224 struct regmap
*regmap
;
230 pmic
= dev_get_drvdata(pdev
->dev
.parent
);
232 dev_err(dev
, "Failed to get struct intel_soc_pmic pointer\n");
236 thermal_data
= (struct pmic_thermal_data
*)
237 platform_get_device_id(pdev
)->driver_data
;
239 dev_err(dev
, "No thermal data initialized!!\n");
243 regmap
= pmic
->regmap
;
244 regmap_irq_chip
= pmic
->irq_chip_data_level2
;
247 while ((irq
= platform_get_irq(pdev
, pmic_irq_count
)) != -ENXIO
) {
248 virq
= regmap_irq_get_virq(regmap_irq_chip
, irq
);
250 dev_err(dev
, "failed to get virq by irq %d\n", irq
);
254 ret
= devm_request_threaded_irq(&pdev
->dev
, virq
,
255 NULL
, pmic_thermal_irq_handler
,
256 IRQF_ONESHOT
, "pmic_thermal", pdev
);
259 dev_err(dev
, "request irq(%d) failed: %d\n", virq
, ret
);
265 /* Enable thermal interrupts */
266 for (i
= 0; i
< thermal_data
->num_maps
; i
++) {
267 for (j
= 0; j
< thermal_data
->maps
[i
].num_trips
; j
++) {
268 reg
= thermal_data
->maps
[i
].trip_config
[j
].irq_en
;
269 mask
= thermal_data
->maps
[i
].trip_config
[j
].irq_en_mask
;
270 ret
= regmap_update_bits(regmap
, reg
, mask
, 0x00);
279 static const struct platform_device_id pmic_thermal_id_table
[] = {
281 .name
= "bxt_wcove_thermal",
282 .driver_data
= (kernel_ulong_t
)&bxtwc_thermal_data
,
287 static struct platform_driver pmic_thermal_driver
= {
288 .probe
= pmic_thermal_probe
,
290 .name
= "pmic_thermal",
292 .id_table
= pmic_thermal_id_table
,
295 MODULE_DEVICE_TABLE(platform
, pmic_thermal_id_table
);
296 module_platform_driver(pmic_thermal_driver
);
298 MODULE_AUTHOR("Yegnesh S Iyer <yegnesh.s.iyer@intel.com>");
299 MODULE_DESCRIPTION("Intel Broxton PMIC Thermal Driver");
300 MODULE_LICENSE("GPL v2");