2 * ST Thermal Sensor Driver for memory mapped sensors.
3 * Author: Ajit Pal Singh <ajitpal.singh@st.com>
5 * Copyright (C) 2003-2014 STMicroelectronics (R&D) Limited
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
14 #include <linux/module.h>
16 #include "st_thermal.h"
18 #define STIH416_MPE_CONF 0x0
19 #define STIH416_MPE_STATUS 0x4
20 #define STIH416_MPE_INT_THRESH 0x8
21 #define STIH416_MPE_INT_EN 0xC
23 /* Power control bits for the memory mapped thermal sensor */
24 #define THERMAL_PDN BIT(4)
25 #define THERMAL_SRSTN BIT(10)
27 static const struct reg_field st_mmap_thermal_regfields
[MAX_REGFIELDS
] = {
29 * According to the STIH416 MPE temp sensor data sheet -
30 * the PDN (Power Down Bit) and SRSTN (Soft Reset Bit) need to be
31 * written simultaneously for powering on and off the temperature
32 * sensor. regmap_update_bits() will be used to update the register.
34 [INT_THRESH_HI
] = REG_FIELD(STIH416_MPE_INT_THRESH
, 0, 7),
35 [DCORRECT
] = REG_FIELD(STIH416_MPE_CONF
, 5, 9),
36 [OVERFLOW
] = REG_FIELD(STIH416_MPE_STATUS
, 9, 9),
37 [DATA
] = REG_FIELD(STIH416_MPE_STATUS
, 11, 18),
38 [INT_ENABLE
] = REG_FIELD(STIH416_MPE_INT_EN
, 0, 0),
41 static irqreturn_t
st_mmap_thermal_trip_handler(int irq
, void *sdata
)
43 struct st_thermal_sensor
*sensor
= sdata
;
45 thermal_zone_device_update(sensor
->thermal_dev
);
50 /* Private ops for the Memory Mapped based thermal sensors */
51 static int st_mmap_power_ctrl(struct st_thermal_sensor
*sensor
,
52 enum st_thermal_power_state power_state
)
54 const unsigned int mask
= (THERMAL_PDN
| THERMAL_SRSTN
);
55 const unsigned int val
= power_state
? mask
: 0;
57 return regmap_update_bits(sensor
->regmap
, STIH416_MPE_CONF
, mask
, val
);
60 static int st_mmap_alloc_regfields(struct st_thermal_sensor
*sensor
)
62 struct device
*dev
= sensor
->dev
;
63 struct regmap
*regmap
= sensor
->regmap
;
64 const struct reg_field
*reg_fields
= sensor
->cdata
->reg_fields
;
66 sensor
->int_thresh_hi
= devm_regmap_field_alloc(dev
, regmap
,
67 reg_fields
[INT_THRESH_HI
]);
68 sensor
->int_enable
= devm_regmap_field_alloc(dev
, regmap
,
69 reg_fields
[INT_ENABLE
]);
71 if (IS_ERR(sensor
->int_thresh_hi
) || IS_ERR(sensor
->int_enable
)) {
72 dev_err(dev
, "failed to alloc mmap regfields\n");
79 static int st_mmap_enable_irq(struct st_thermal_sensor
*sensor
)
83 /* Set upper critical threshold */
84 ret
= regmap_field_write(sensor
->int_thresh_hi
,
85 sensor
->cdata
->crit_temp
-
86 sensor
->cdata
->temp_adjust_val
);
90 return regmap_field_write(sensor
->int_enable
, 1);
93 static int st_mmap_register_enable_irq(struct st_thermal_sensor
*sensor
)
95 struct device
*dev
= sensor
->dev
;
96 struct platform_device
*pdev
= to_platform_device(dev
);
99 sensor
->irq
= platform_get_irq(pdev
, 0);
100 if (sensor
->irq
< 0) {
101 dev_err(dev
, "failed to register IRQ\n");
105 ret
= devm_request_threaded_irq(dev
, sensor
->irq
,
106 NULL
, st_mmap_thermal_trip_handler
,
107 IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
108 dev
->driver
->name
, sensor
);
110 dev_err(dev
, "failed to register IRQ %d\n", sensor
->irq
);
114 return st_mmap_enable_irq(sensor
);
117 static const struct regmap_config st_416mpe_regmap_config
= {
123 static int st_mmap_regmap_init(struct st_thermal_sensor
*sensor
)
125 struct device
*dev
= sensor
->dev
;
126 struct platform_device
*pdev
= to_platform_device(dev
);
127 struct resource
*res
;
129 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
131 dev_err(dev
, "no memory resources defined\n");
135 sensor
->mmio_base
= devm_ioremap_resource(dev
, res
);
136 if (IS_ERR(sensor
->mmio_base
)) {
137 dev_err(dev
, "failed to remap IO\n");
138 return PTR_ERR(sensor
->mmio_base
);
141 sensor
->regmap
= devm_regmap_init_mmio(dev
, sensor
->mmio_base
,
142 &st_416mpe_regmap_config
);
143 if (IS_ERR(sensor
->regmap
)) {
144 dev_err(dev
, "failed to initialise regmap\n");
145 return PTR_ERR(sensor
->regmap
);
151 static const struct st_thermal_sensor_ops st_mmap_sensor_ops
= {
152 .power_ctrl
= st_mmap_power_ctrl
,
153 .alloc_regfields
= st_mmap_alloc_regfields
,
154 .regmap_init
= st_mmap_regmap_init
,
155 .register_enable_irq
= st_mmap_register_enable_irq
,
156 .enable_irq
= st_mmap_enable_irq
,
159 /* Compatible device data stih416 mpe thermal sensor */
160 static const struct st_thermal_compat_data st_416mpe_cdata
= {
161 .reg_fields
= st_mmap_thermal_regfields
,
162 .ops
= &st_mmap_sensor_ops
,
163 .calibration_val
= 14,
164 .temp_adjust_val
= -95,
168 /* Compatible device data stih407 thermal sensor */
169 static const struct st_thermal_compat_data st_407_cdata
= {
170 .reg_fields
= st_mmap_thermal_regfields
,
171 .ops
= &st_mmap_sensor_ops
,
172 .calibration_val
= 16,
173 .temp_adjust_val
= -95,
177 static const struct of_device_id st_mmap_thermal_of_match
[] = {
178 { .compatible
= "st,stih416-mpe-thermal", .data
= &st_416mpe_cdata
},
179 { .compatible
= "st,stih407-thermal", .data
= &st_407_cdata
},
182 MODULE_DEVICE_TABLE(of
, st_mmap_thermal_of_match
);
184 static int st_mmap_probe(struct platform_device
*pdev
)
186 return st_thermal_register(pdev
, st_mmap_thermal_of_match
);
189 static int st_mmap_remove(struct platform_device
*pdev
)
191 return st_thermal_unregister(pdev
);
194 static struct platform_driver st_mmap_thermal_driver
= {
196 .name
= "st_thermal_mmap",
197 .pm
= &st_thermal_pm_ops
,
198 .of_match_table
= st_mmap_thermal_of_match
,
200 .probe
= st_mmap_probe
,
201 .remove
= st_mmap_remove
,
204 module_platform_driver(st_mmap_thermal_driver
);
206 MODULE_AUTHOR("STMicroelectronics (R&D) Limited <ajitpal.singh@st.com>");
207 MODULE_DESCRIPTION("STMicroelectronics STi SoC Thermal Sensor Driver");
208 MODULE_LICENSE("GPL v2");