1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2021 Emil Renner Berthing <kernel@esmil.dk>
4 * Copyright (C) 2021 Samin Guo <samin.guo@starfivetech.com>
7 #include <linux/bits.h>
9 #include <linux/delay.h>
10 #include <linux/hwmon.h>
12 #include <linux/module.h>
13 #include <linux/mutex.h>
15 #include <linux/platform_device.h>
16 #include <linux/reset.h>
19 * TempSensor reset. The RSTN can be de-asserted once the analog core has
20 * powered up. Trst(min 100ns)
23 #define SFCTEMP_RSTN BIT(0)
26 * TempSensor analog core power down. The analog core will be powered up
27 * Tpu(min 50us) after PD is de-asserted. RSTN should be held low until the
28 * analog core is powered up.
29 * 0:power up 1:power down
31 #define SFCTEMP_PD BIT(1)
34 * TempSensor start conversion enable.
37 #define SFCTEMP_RUN BIT(2)
40 * TempSensor conversion value output.
41 * Temp(C)=DOUT*Y/4094 - K
43 #define SFCTEMP_DOUT_POS 16
44 #define SFCTEMP_DOUT_MSK GENMASK(27, 16)
46 /* DOUT to Celcius conversion constants */
47 #define SFCTEMP_Y1000 237500L
48 #define SFCTEMP_Z 4094L
49 #define SFCTEMP_K1000 81100L
52 /* serialize access to hardware register and enabled below */
55 struct clk
*clk_sense
;
57 struct reset_control
*rst_sense
;
58 struct reset_control
*rst_bus
;
62 static void sfctemp_power_up(struct sfctemp
*sfctemp
)
64 /* make sure we're powered down first */
65 writel(SFCTEMP_PD
, sfctemp
->regs
);
68 writel(0, sfctemp
->regs
);
69 /* wait t_pu(50us) + t_rst(100ns) */
70 usleep_range(60, 200);
73 writel(SFCTEMP_RSTN
, sfctemp
->regs
);
74 udelay(1); /* wait t_su(500ps) */
77 static void sfctemp_power_down(struct sfctemp
*sfctemp
)
79 writel(SFCTEMP_PD
, sfctemp
->regs
);
82 static void sfctemp_run(struct sfctemp
*sfctemp
)
84 writel(SFCTEMP_RSTN
| SFCTEMP_RUN
, sfctemp
->regs
);
88 static void sfctemp_stop(struct sfctemp
*sfctemp
)
90 writel(SFCTEMP_RSTN
, sfctemp
->regs
);
93 static int sfctemp_enable(struct sfctemp
*sfctemp
)
97 mutex_lock(&sfctemp
->lock
);
101 ret
= clk_prepare_enable(sfctemp
->clk_bus
);
104 ret
= reset_control_deassert(sfctemp
->rst_bus
);
106 goto err_disable_bus
;
108 ret
= clk_prepare_enable(sfctemp
->clk_sense
);
111 ret
= reset_control_deassert(sfctemp
->rst_sense
);
113 goto err_disable_sense
;
115 sfctemp_power_up(sfctemp
);
116 sfctemp_run(sfctemp
);
117 sfctemp
->enabled
= true;
119 mutex_unlock(&sfctemp
->lock
);
123 clk_disable_unprepare(sfctemp
->clk_sense
);
125 reset_control_assert(sfctemp
->rst_bus
);
127 clk_disable_unprepare(sfctemp
->clk_bus
);
129 mutex_unlock(&sfctemp
->lock
);
133 static int sfctemp_disable(struct sfctemp
*sfctemp
)
135 mutex_lock(&sfctemp
->lock
);
136 if (!sfctemp
->enabled
)
139 sfctemp_stop(sfctemp
);
140 sfctemp_power_down(sfctemp
);
141 reset_control_assert(sfctemp
->rst_sense
);
142 clk_disable_unprepare(sfctemp
->clk_sense
);
143 reset_control_assert(sfctemp
->rst_bus
);
144 clk_disable_unprepare(sfctemp
->clk_bus
);
145 sfctemp
->enabled
= false;
147 mutex_unlock(&sfctemp
->lock
);
151 static void sfctemp_disable_action(void *data
)
153 sfctemp_disable(data
);
156 static int sfctemp_convert(struct sfctemp
*sfctemp
, long *val
)
160 mutex_lock(&sfctemp
->lock
);
161 if (!sfctemp
->enabled
) {
166 /* calculate temperature in milli Celcius */
167 *val
= (long)((readl(sfctemp
->regs
) & SFCTEMP_DOUT_MSK
) >> SFCTEMP_DOUT_POS
)
168 * SFCTEMP_Y1000
/ SFCTEMP_Z
- SFCTEMP_K1000
;
172 mutex_unlock(&sfctemp
->lock
);
176 static umode_t
sfctemp_is_visible(const void *data
, enum hwmon_sensor_types type
,
177 u32 attr
, int channel
)
182 case hwmon_temp_enable
:
184 case hwmon_temp_input
:
194 static int sfctemp_read(struct device
*dev
, enum hwmon_sensor_types type
,
195 u32 attr
, int channel
, long *val
)
197 struct sfctemp
*sfctemp
= dev_get_drvdata(dev
);
202 case hwmon_temp_enable
:
203 *val
= sfctemp
->enabled
;
205 case hwmon_temp_input
:
206 return sfctemp_convert(sfctemp
, val
);
215 static int sfctemp_write(struct device
*dev
, enum hwmon_sensor_types type
,
216 u32 attr
, int channel
, long val
)
218 struct sfctemp
*sfctemp
= dev_get_drvdata(dev
);
223 case hwmon_temp_enable
:
225 return sfctemp_disable(sfctemp
);
227 return sfctemp_enable(sfctemp
);
237 static const struct hwmon_channel_info
*sfctemp_info
[] = {
238 HWMON_CHANNEL_INFO(chip
, HWMON_C_REGISTER_TZ
),
239 HWMON_CHANNEL_INFO(temp
, HWMON_T_ENABLE
| HWMON_T_INPUT
),
243 static const struct hwmon_ops sfctemp_hwmon_ops
= {
244 .is_visible
= sfctemp_is_visible
,
245 .read
= sfctemp_read
,
246 .write
= sfctemp_write
,
249 static const struct hwmon_chip_info sfctemp_chip_info
= {
250 .ops
= &sfctemp_hwmon_ops
,
251 .info
= sfctemp_info
,
254 static int sfctemp_probe(struct platform_device
*pdev
)
256 struct device
*dev
= &pdev
->dev
;
257 struct device
*hwmon_dev
;
258 struct sfctemp
*sfctemp
;
261 sfctemp
= devm_kzalloc(dev
, sizeof(*sfctemp
), GFP_KERNEL
);
265 dev_set_drvdata(dev
, sfctemp
);
266 mutex_init(&sfctemp
->lock
);
268 sfctemp
->regs
= devm_platform_ioremap_resource(pdev
, 0);
269 if (IS_ERR(sfctemp
->regs
))
270 return PTR_ERR(sfctemp
->regs
);
272 sfctemp
->clk_sense
= devm_clk_get(dev
, "sense");
273 if (IS_ERR(sfctemp
->clk_sense
))
274 return dev_err_probe(dev
, PTR_ERR(sfctemp
->clk_sense
),
275 "error getting sense clock\n");
277 sfctemp
->clk_bus
= devm_clk_get(dev
, "bus");
278 if (IS_ERR(sfctemp
->clk_bus
))
279 return dev_err_probe(dev
, PTR_ERR(sfctemp
->clk_bus
),
280 "error getting bus clock\n");
282 sfctemp
->rst_sense
= devm_reset_control_get_exclusive(dev
, "sense");
283 if (IS_ERR(sfctemp
->rst_sense
))
284 return dev_err_probe(dev
, PTR_ERR(sfctemp
->rst_sense
),
285 "error getting sense reset\n");
287 sfctemp
->rst_bus
= devm_reset_control_get_exclusive(dev
, "bus");
288 if (IS_ERR(sfctemp
->rst_bus
))
289 return dev_err_probe(dev
, PTR_ERR(sfctemp
->rst_bus
),
290 "error getting busreset\n");
292 ret
= reset_control_assert(sfctemp
->rst_sense
);
294 return dev_err_probe(dev
, ret
, "error asserting sense reset\n");
296 ret
= reset_control_assert(sfctemp
->rst_bus
);
298 return dev_err_probe(dev
, ret
, "error asserting bus reset\n");
300 ret
= devm_add_action(dev
, sfctemp_disable_action
, sfctemp
);
304 ret
= sfctemp_enable(sfctemp
);
306 return dev_err_probe(dev
, ret
, "error enabling temperature sensor\n");
308 hwmon_dev
= devm_hwmon_device_register_with_info(dev
, "sfctemp", sfctemp
,
309 &sfctemp_chip_info
, NULL
);
310 return PTR_ERR_OR_ZERO(hwmon_dev
);
313 static const struct of_device_id sfctemp_of_match
[] = {
314 { .compatible
= "starfive,jh7100-temp" },
315 { .compatible
= "starfive,jh7110-temp" },
318 MODULE_DEVICE_TABLE(of
, sfctemp_of_match
);
320 static struct platform_driver sfctemp_driver
= {
321 .probe
= sfctemp_probe
,
324 .of_match_table
= sfctemp_of_match
,
327 module_platform_driver(sfctemp_driver
);
329 MODULE_AUTHOR("Emil Renner Berthing");
330 MODULE_DESCRIPTION("StarFive JH71x0 temperature sensor driver");
331 MODULE_LICENSE("GPL");