1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2020 BAIKAL ELECTRONICS, JSC
6 * Maxim Kaurkin <maxim.kaurkin@baikalelectronics.ru>
7 * Serge Semin <Sergey.Semin@baikalelectronics.ru>
9 * Baikal-T1 Process, Voltage, Temperature sensor driver
12 #include <linux/bitfield.h>
13 #include <linux/bitops.h>
14 #include <linux/clk.h>
15 #include <linux/completion.h>
16 #include <linux/delay.h>
17 #include <linux/device.h>
18 #include <linux/hwmon-sysfs.h>
19 #include <linux/hwmon.h>
20 #include <linux/interrupt.h>
22 #include <linux/kernel.h>
23 #include <linux/ktime.h>
24 #include <linux/limits.h>
25 #include <linux/module.h>
26 #include <linux/mutex.h>
28 #include <linux/platform_device.h>
29 #include <linux/seqlock.h>
30 #include <linux/sysfs.h>
31 #include <linux/types.h>
36 * For the sake of the code simplification we created the sensors info table
37 * with the sensor names, activation modes, threshold registers base address
38 * and the thresholds bit fields.
40 static const struct pvt_sensor_info pvt_info
[] = {
41 PVT_SENSOR_INFO(0, "CPU Core Temperature", hwmon_temp
, TEMP
, TTHRES
),
42 PVT_SENSOR_INFO(0, "CPU Core Voltage", hwmon_in
, VOLT
, VTHRES
),
43 PVT_SENSOR_INFO(1, "CPU Core Low-Vt", hwmon_in
, LVT
, LTHRES
),
44 PVT_SENSOR_INFO(2, "CPU Core High-Vt", hwmon_in
, HVT
, HTHRES
),
45 PVT_SENSOR_INFO(3, "CPU Core Standard-Vt", hwmon_in
, SVT
, STHRES
),
49 * The original translation formulae of the temperature (in degrees of Celsius)
50 * to PVT data and vice-versa are following:
51 * N = 1.8322e-8*(T^4) + 2.343e-5*(T^3) + 8.7018e-3*(T^2) + 3.9269*(T^1) +
53 * T = -1.6743e-11*(N^4) + 8.1542e-8*(N^3) + -1.8201e-4*(N^2) +
54 * 3.1020e-1*(N^1) - 4.838e1,
55 * where T = [-48.380, 147.438]C and N = [0, 1023].
56 * They must be accordingly altered to be suitable for the integer arithmetics.
57 * The technique is called 'factor redistribution', which just makes sure the
58 * multiplications and divisions are made so to have a result of the operations
59 * within the integer numbers limit. In addition we need to translate the
60 * formulae to accept millidegrees of Celsius. Here what they look like after
62 * N = (18322e-20*(T^4) + 2343e-13*(T^3) + 87018e-9*(T^2) + 39269e-3*T +
64 * T = -16743e-12*(D^4) + 81542e-9*(D^3) - 182010e-6*(D^2) + 310200e-3*D -
66 * where T = [-48380, 147438] mC and N = [0, 1023].
68 static const struct pvt_poly __maybe_unused poly_temp_to_N
= {
69 .total_divider
= 10000,
71 {4, 18322, 10000, 10000},
73 {2, 87018, 10000, 10},
79 static const struct pvt_poly poly_N_to_temp
= {
84 {2, -182010, 1000, 1},
91 * Similar alterations are performed for the voltage conversion equations.
92 * The original formulae are:
93 * N = 1.8658e3*V - 1.1572e3,
94 * V = (N + 1.1572e3) / 1.8658e3,
95 * where V = [0.620, 1.168] V and N = [0, 1023].
96 * After the optimization they looks as follows:
97 * N = (18658e-3*V - 11572) / 10,
98 * V = N * 10^5 / 18658 + 11572 * 10^4 / 18658.
100 static const struct pvt_poly __maybe_unused poly_volt_to_N
= {
108 static const struct pvt_poly poly_N_to_volt
= {
111 {1, 100000, 18658, 1},
112 {0, 115720000, 1, 18658}
117 * Here is the polynomial calculation function, which performs the
118 * redistributed terms calculations. It's pretty straightforward. We walk
119 * over each degree term up to the free one, and perform the redistributed
120 * multiplication of the term coefficient, its divider (as for the rationale
121 * fraction representation), data power and the rational fraction divider
122 * leftover. Then all of this is collected in a total sum variable, which
123 * value is normalized by the total divider before being returned.
125 static long pvt_calc_poly(const struct pvt_poly
*poly
, long data
)
127 const struct pvt_poly_term
*term
= poly
->terms
;
133 for (deg
= 0; deg
< term
->deg
; ++deg
)
134 tmp
= mult_frac(tmp
, data
, term
->divider
);
135 ret
+= tmp
/ term
->divider_leftover
;
136 } while ((term
++)->deg
);
138 return ret
/ poly
->total_divider
;
141 static inline u32
pvt_update(void __iomem
*reg
, u32 mask
, u32 data
)
145 old
= readl_relaxed(reg
);
146 writel((old
& ~mask
) | (data
& mask
), reg
);
152 * Baikal-T1 PVT mode can be updated only when the controller is disabled.
153 * So first we disable it, then set the new mode together with the controller
154 * getting back enabled. The same concerns the temperature trim and
155 * measurements timeout. If it is necessary the interface mutex is supposed
156 * to be locked at the time the operations are performed.
158 static inline void pvt_set_mode(struct pvt_hwmon
*pvt
, u32 mode
)
162 mode
= FIELD_PREP(PVT_CTRL_MODE_MASK
, mode
);
164 old
= pvt_update(pvt
->regs
+ PVT_CTRL
, PVT_CTRL_EN
, 0);
165 pvt_update(pvt
->regs
+ PVT_CTRL
, PVT_CTRL_MODE_MASK
| PVT_CTRL_EN
,
169 static inline u32
pvt_calc_trim(long temp
)
171 temp
= clamp_val(temp
, 0, PVT_TRIM_TEMP
);
173 return DIV_ROUND_UP(temp
, PVT_TRIM_STEP
);
176 static inline void pvt_set_trim(struct pvt_hwmon
*pvt
, u32 trim
)
180 trim
= FIELD_PREP(PVT_CTRL_TRIM_MASK
, trim
);
182 old
= pvt_update(pvt
->regs
+ PVT_CTRL
, PVT_CTRL_EN
, 0);
183 pvt_update(pvt
->regs
+ PVT_CTRL
, PVT_CTRL_TRIM_MASK
| PVT_CTRL_EN
,
187 static inline void pvt_set_tout(struct pvt_hwmon
*pvt
, u32 tout
)
191 old
= pvt_update(pvt
->regs
+ PVT_CTRL
, PVT_CTRL_EN
, 0);
192 writel(tout
, pvt
->regs
+ PVT_TTIMEOUT
);
193 pvt_update(pvt
->regs
+ PVT_CTRL
, PVT_CTRL_EN
, old
);
197 * This driver can optionally provide the hwmon alarms for each sensor the PVT
198 * controller supports. The alarms functionality is made compile-time
199 * configurable due to the hardware interface implementation peculiarity
200 * described further in this comment. So in case if alarms are unnecessary in
201 * your system design it's recommended to have them disabled to prevent the PVT
202 * IRQs being periodically raised to get the data cache/alarms status up to
205 * Baikal-T1 PVT embedded controller is based on the Analog Bits PVT sensor,
206 * but is equipped with a dedicated control wrapper. It exposes the PVT
207 * sub-block registers space via the APB3 bus. In addition the wrapper provides
208 * a common interrupt vector of the sensors conversion completion events and
209 * threshold value alarms. Alas the wrapper interface hasn't been fully thought
210 * through. There is only one sensor can be activated at a time, for which the
211 * thresholds comparator is enabled right after the data conversion is
212 * completed. Due to this if alarms need to be implemented for all available
213 * sensors we can't just set the thresholds and enable the interrupts. We need
214 * to enable the sensors one after another and let the controller to detect
215 * the alarms by itself at each conversion. This also makes pointless to handle
216 * the alarms interrupts, since in occasion they happen synchronously with
217 * data conversion completion. The best driver design would be to have the
218 * completion interrupts enabled only and keep the converted value in the
219 * driver data cache. This solution is implemented if hwmon alarms are enabled
220 * in this driver. In case if the alarms are disabled, the conversion is
221 * performed on demand at the time a sensors input file is read.
224 #if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
226 #define pvt_hard_isr NULL
228 static irqreturn_t
pvt_soft_isr(int irq
, void *data
)
230 const struct pvt_sensor_info
*info
;
231 struct pvt_hwmon
*pvt
= data
;
232 struct pvt_cache
*cache
;
233 u32 val
, thres_sts
, old
;
236 * DVALID bit will be cleared by reading the data. We need to save the
237 * status before the next conversion happens. Threshold events will be
238 * handled a bit later.
240 thres_sts
= readl(pvt
->regs
+ PVT_RAW_INTR_STAT
);
243 * Then lets recharge the PVT interface with the next sampling mode.
244 * Lock the interface mutex to serialize trim, timeouts and alarm
245 * thresholds settings.
247 cache
= &pvt
->cache
[pvt
->sensor
];
248 info
= &pvt_info
[pvt
->sensor
];
249 pvt
->sensor
= (pvt
->sensor
== PVT_SENSOR_LAST
) ?
250 PVT_SENSOR_FIRST
: (pvt
->sensor
+ 1);
253 * For some reason we have to mask the interrupt before changing the
254 * mode, otherwise sometimes the temperature mode doesn't get
255 * activated even though the actual mode in the ctrl register
256 * corresponds to one. Then we read the data. By doing so we also
257 * recharge the data conversion. After this the mode corresponding
258 * to the next sensor in the row is set. Finally we enable the
261 mutex_lock(&pvt
->iface_mtx
);
263 old
= pvt_update(pvt
->regs
+ PVT_INTR_MASK
, PVT_INTR_DVALID
,
266 val
= readl(pvt
->regs
+ PVT_DATA
);
268 pvt_set_mode(pvt
, pvt_info
[pvt
->sensor
].mode
);
270 pvt_update(pvt
->regs
+ PVT_INTR_MASK
, PVT_INTR_DVALID
, old
);
272 mutex_unlock(&pvt
->iface_mtx
);
275 * We can now update the data cache with data just retrieved from the
276 * sensor. Lock write-seqlock to make sure the reader has a coherent
279 write_seqlock(&cache
->data_seqlock
);
281 cache
->data
= FIELD_GET(PVT_DATA_DATA_MASK
, val
);
283 write_sequnlock(&cache
->data_seqlock
);
286 * While PVT core is doing the next mode data conversion, we'll check
287 * whether the alarms were triggered for the current sensor. Note that
288 * according to the documentation only one threshold IRQ status can be
289 * set at a time, that's why if-else statement is utilized.
291 if ((thres_sts
& info
->thres_sts_lo
) ^ cache
->thres_sts_lo
) {
292 WRITE_ONCE(cache
->thres_sts_lo
, thres_sts
& info
->thres_sts_lo
);
293 hwmon_notify_event(pvt
->hwmon
, info
->type
, info
->attr_min_alarm
,
295 } else if ((thres_sts
& info
->thres_sts_hi
) ^ cache
->thres_sts_hi
) {
296 WRITE_ONCE(cache
->thres_sts_hi
, thres_sts
& info
->thres_sts_hi
);
297 hwmon_notify_event(pvt
->hwmon
, info
->type
, info
->attr_max_alarm
,
304 static inline umode_t
pvt_limit_is_visible(enum pvt_sensor_type type
)
309 static inline umode_t
pvt_alarm_is_visible(enum pvt_sensor_type type
)
314 static int pvt_read_data(struct pvt_hwmon
*pvt
, enum pvt_sensor_type type
,
317 struct pvt_cache
*cache
= &pvt
->cache
[type
];
322 seq
= read_seqbegin(&cache
->data_seqlock
);
324 } while (read_seqretry(&cache
->data_seqlock
, seq
));
326 if (type
== PVT_TEMP
)
327 *val
= pvt_calc_poly(&poly_N_to_temp
, data
);
329 *val
= pvt_calc_poly(&poly_N_to_volt
, data
);
334 static int pvt_read_limit(struct pvt_hwmon
*pvt
, enum pvt_sensor_type type
,
335 bool is_low
, long *val
)
339 /* No need in serialization, since it is just read from MMIO. */
340 data
= readl(pvt
->regs
+ pvt_info
[type
].thres_base
);
343 data
= FIELD_GET(PVT_THRES_LO_MASK
, data
);
345 data
= FIELD_GET(PVT_THRES_HI_MASK
, data
);
347 if (type
== PVT_TEMP
)
348 *val
= pvt_calc_poly(&poly_N_to_temp
, data
);
350 *val
= pvt_calc_poly(&poly_N_to_volt
, data
);
355 static int pvt_write_limit(struct pvt_hwmon
*pvt
, enum pvt_sensor_type type
,
356 bool is_low
, long val
)
358 u32 data
, limit
, mask
;
361 if (type
== PVT_TEMP
) {
362 val
= clamp(val
, PVT_TEMP_MIN
, PVT_TEMP_MAX
);
363 data
= pvt_calc_poly(&poly_temp_to_N
, val
);
365 val
= clamp(val
, PVT_VOLT_MIN
, PVT_VOLT_MAX
);
366 data
= pvt_calc_poly(&poly_volt_to_N
, val
);
369 /* Serialize limit update, since a part of the register is changed. */
370 ret
= mutex_lock_interruptible(&pvt
->iface_mtx
);
374 /* Make sure the upper and lower ranges don't intersect. */
375 limit
= readl(pvt
->regs
+ pvt_info
[type
].thres_base
);
377 limit
= FIELD_GET(PVT_THRES_HI_MASK
, limit
);
378 data
= clamp_val(data
, PVT_DATA_MIN
, limit
);
379 data
= FIELD_PREP(PVT_THRES_LO_MASK
, data
);
380 mask
= PVT_THRES_LO_MASK
;
382 limit
= FIELD_GET(PVT_THRES_LO_MASK
, limit
);
383 data
= clamp_val(data
, limit
, PVT_DATA_MAX
);
384 data
= FIELD_PREP(PVT_THRES_HI_MASK
, data
);
385 mask
= PVT_THRES_HI_MASK
;
388 pvt_update(pvt
->regs
+ pvt_info
[type
].thres_base
, mask
, data
);
390 mutex_unlock(&pvt
->iface_mtx
);
395 static int pvt_read_alarm(struct pvt_hwmon
*pvt
, enum pvt_sensor_type type
,
396 bool is_low
, long *val
)
399 *val
= !!READ_ONCE(pvt
->cache
[type
].thres_sts_lo
);
401 *val
= !!READ_ONCE(pvt
->cache
[type
].thres_sts_hi
);
406 static const struct hwmon_channel_info
*pvt_channel_info
[] = {
407 HWMON_CHANNEL_INFO(chip
,
408 HWMON_C_REGISTER_TZ
| HWMON_C_UPDATE_INTERVAL
),
409 HWMON_CHANNEL_INFO(temp
,
410 HWMON_T_INPUT
| HWMON_T_TYPE
| HWMON_T_LABEL
|
411 HWMON_T_MIN
| HWMON_T_MIN_ALARM
|
412 HWMON_T_MAX
| HWMON_T_MAX_ALARM
|
414 HWMON_CHANNEL_INFO(in
,
415 HWMON_I_INPUT
| HWMON_I_LABEL
|
416 HWMON_I_MIN
| HWMON_I_MIN_ALARM
|
417 HWMON_I_MAX
| HWMON_I_MAX_ALARM
,
418 HWMON_I_INPUT
| HWMON_I_LABEL
|
419 HWMON_I_MIN
| HWMON_I_MIN_ALARM
|
420 HWMON_I_MAX
| HWMON_I_MAX_ALARM
,
421 HWMON_I_INPUT
| HWMON_I_LABEL
|
422 HWMON_I_MIN
| HWMON_I_MIN_ALARM
|
423 HWMON_I_MAX
| HWMON_I_MAX_ALARM
,
424 HWMON_I_INPUT
| HWMON_I_LABEL
|
425 HWMON_I_MIN
| HWMON_I_MIN_ALARM
|
426 HWMON_I_MAX
| HWMON_I_MAX_ALARM
),
430 #else /* !CONFIG_SENSORS_BT1_PVT_ALARMS */
432 static irqreturn_t
pvt_hard_isr(int irq
, void *data
)
434 struct pvt_hwmon
*pvt
= data
;
435 struct pvt_cache
*cache
;
439 * Mask the DVALID interrupt so after exiting from the handler a
440 * repeated conversion wouldn't happen.
442 pvt_update(pvt
->regs
+ PVT_INTR_MASK
, PVT_INTR_DVALID
,
446 * Nothing special for alarm-less driver. Just read the data, update
447 * the cache and notify a waiter of this event.
449 val
= readl(pvt
->regs
+ PVT_DATA
);
450 if (!(val
& PVT_DATA_VALID
)) {
451 dev_err(pvt
->dev
, "Got IRQ when data isn't valid\n");
455 cache
= &pvt
->cache
[pvt
->sensor
];
457 WRITE_ONCE(cache
->data
, FIELD_GET(PVT_DATA_DATA_MASK
, val
));
459 complete(&cache
->conversion
);
464 #define pvt_soft_isr NULL
466 static inline umode_t
pvt_limit_is_visible(enum pvt_sensor_type type
)
471 static inline umode_t
pvt_alarm_is_visible(enum pvt_sensor_type type
)
476 static int pvt_read_data(struct pvt_hwmon
*pvt
, enum pvt_sensor_type type
,
479 struct pvt_cache
*cache
= &pvt
->cache
[type
];
480 unsigned long timeout
;
485 * Lock PVT conversion interface until data cache is updated. The
486 * data read procedure is following: set the requested PVT sensor
487 * mode, enable IRQ and conversion, wait until conversion is finished,
488 * then disable conversion and IRQ, and read the cached data.
490 ret
= mutex_lock_interruptible(&pvt
->iface_mtx
);
495 pvt_set_mode(pvt
, pvt_info
[type
].mode
);
498 * Unmask the DVALID interrupt and enable the sensors conversions.
499 * Do the reverse procedure when conversion is done.
501 pvt_update(pvt
->regs
+ PVT_INTR_MASK
, PVT_INTR_DVALID
, 0);
502 pvt_update(pvt
->regs
+ PVT_CTRL
, PVT_CTRL_EN
, PVT_CTRL_EN
);
505 * Wait with timeout since in case if the sensor is suddenly powered
506 * down the request won't be completed and the caller will hang up on
507 * this procedure until the power is back up again. Multiply the
508 * timeout by the factor of two to prevent a false timeout.
510 timeout
= 2 * usecs_to_jiffies(ktime_to_us(pvt
->timeout
));
511 ret
= wait_for_completion_timeout(&cache
->conversion
, timeout
);
513 pvt_update(pvt
->regs
+ PVT_CTRL
, PVT_CTRL_EN
, 0);
514 pvt_update(pvt
->regs
+ PVT_INTR_MASK
, PVT_INTR_DVALID
,
517 data
= READ_ONCE(cache
->data
);
519 mutex_unlock(&pvt
->iface_mtx
);
524 if (type
== PVT_TEMP
)
525 *val
= pvt_calc_poly(&poly_N_to_temp
, data
);
527 *val
= pvt_calc_poly(&poly_N_to_volt
, data
);
532 static int pvt_read_limit(struct pvt_hwmon
*pvt
, enum pvt_sensor_type type
,
533 bool is_low
, long *val
)
538 static int pvt_write_limit(struct pvt_hwmon
*pvt
, enum pvt_sensor_type type
,
539 bool is_low
, long val
)
544 static int pvt_read_alarm(struct pvt_hwmon
*pvt
, enum pvt_sensor_type type
,
545 bool is_low
, long *val
)
550 static const struct hwmon_channel_info
*pvt_channel_info
[] = {
551 HWMON_CHANNEL_INFO(chip
,
552 HWMON_C_REGISTER_TZ
| HWMON_C_UPDATE_INTERVAL
),
553 HWMON_CHANNEL_INFO(temp
,
554 HWMON_T_INPUT
| HWMON_T_TYPE
| HWMON_T_LABEL
|
556 HWMON_CHANNEL_INFO(in
,
557 HWMON_I_INPUT
| HWMON_I_LABEL
,
558 HWMON_I_INPUT
| HWMON_I_LABEL
,
559 HWMON_I_INPUT
| HWMON_I_LABEL
,
560 HWMON_I_INPUT
| HWMON_I_LABEL
),
564 #endif /* !CONFIG_SENSORS_BT1_PVT_ALARMS */
566 static inline bool pvt_hwmon_channel_is_valid(enum hwmon_sensor_types type
,
571 if (ch
< 0 || ch
>= PVT_TEMP_CHS
)
575 if (ch
< 0 || ch
>= PVT_VOLT_CHS
)
582 /* The rest of the types are independent from the channel number. */
586 static umode_t
pvt_hwmon_is_visible(const void *data
,
587 enum hwmon_sensor_types type
,
590 if (!pvt_hwmon_channel_is_valid(type
, ch
))
596 case hwmon_chip_update_interval
:
602 case hwmon_temp_input
:
603 case hwmon_temp_type
:
604 case hwmon_temp_label
:
608 return pvt_limit_is_visible(ch
);
609 case hwmon_temp_min_alarm
:
610 case hwmon_temp_max_alarm
:
611 return pvt_alarm_is_visible(ch
);
612 case hwmon_temp_offset
:
623 return pvt_limit_is_visible(PVT_VOLT
+ ch
);
624 case hwmon_in_min_alarm
:
625 case hwmon_in_max_alarm
:
626 return pvt_alarm_is_visible(PVT_VOLT
+ ch
);
636 static int pvt_read_trim(struct pvt_hwmon
*pvt
, long *val
)
640 data
= readl(pvt
->regs
+ PVT_CTRL
);
641 *val
= FIELD_GET(PVT_CTRL_TRIM_MASK
, data
) * PVT_TRIM_STEP
;
646 static int pvt_write_trim(struct pvt_hwmon
*pvt
, long val
)
652 * Serialize trim update, since a part of the register is changed and
653 * the controller is supposed to be disabled during this operation.
655 ret
= mutex_lock_interruptible(&pvt
->iface_mtx
);
659 trim
= pvt_calc_trim(val
);
660 pvt_set_trim(pvt
, trim
);
662 mutex_unlock(&pvt
->iface_mtx
);
667 static int pvt_read_timeout(struct pvt_hwmon
*pvt
, long *val
)
671 ret
= mutex_lock_interruptible(&pvt
->iface_mtx
);
675 /* Return the result in msec as hwmon sysfs interface requires. */
676 *val
= ktime_to_ms(pvt
->timeout
);
678 mutex_unlock(&pvt
->iface_mtx
);
683 static int pvt_write_timeout(struct pvt_hwmon
*pvt
, long val
)
690 rate
= clk_get_rate(pvt
->clks
[PVT_CLOCK_REF
].clk
);
695 * If alarms are enabled, the requested timeout must be divided
696 * between all available sensors to have the requested delay
697 * applicable to each individual sensor.
699 cache
= kt
= ms_to_ktime(val
);
700 #if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
701 kt
= ktime_divns(kt
, PVT_SENSORS_NUM
);
705 * Subtract a constant lag, which always persists due to the limited
706 * PVT sampling rate. Make sure the timeout is not negative.
708 kt
= ktime_sub_ns(kt
, PVT_TOUT_MIN
);
709 if (ktime_to_ns(kt
) < 0)
710 kt
= ktime_set(0, 0);
713 * Finally recalculate the timeout in terms of the reference clock
716 data
= ktime_divns(kt
* rate
, NSEC_PER_SEC
);
719 * Update the measurements delay, but lock the interface first, since
720 * we have to disable PVT in order to have the new delay actually
723 ret
= mutex_lock_interruptible(&pvt
->iface_mtx
);
727 pvt_set_tout(pvt
, data
);
728 pvt
->timeout
= cache
;
730 mutex_unlock(&pvt
->iface_mtx
);
735 static int pvt_hwmon_read(struct device
*dev
, enum hwmon_sensor_types type
,
736 u32 attr
, int ch
, long *val
)
738 struct pvt_hwmon
*pvt
= dev_get_drvdata(dev
);
740 if (!pvt_hwmon_channel_is_valid(type
, ch
))
746 case hwmon_chip_update_interval
:
747 return pvt_read_timeout(pvt
, val
);
752 case hwmon_temp_input
:
753 return pvt_read_data(pvt
, ch
, val
);
754 case hwmon_temp_type
:
758 return pvt_read_limit(pvt
, ch
, true, val
);
760 return pvt_read_limit(pvt
, ch
, false, val
);
761 case hwmon_temp_min_alarm
:
762 return pvt_read_alarm(pvt
, ch
, true, val
);
763 case hwmon_temp_max_alarm
:
764 return pvt_read_alarm(pvt
, ch
, false, val
);
765 case hwmon_temp_offset
:
766 return pvt_read_trim(pvt
, val
);
772 return pvt_read_data(pvt
, PVT_VOLT
+ ch
, val
);
774 return pvt_read_limit(pvt
, PVT_VOLT
+ ch
, true, val
);
776 return pvt_read_limit(pvt
, PVT_VOLT
+ ch
, false, val
);
777 case hwmon_in_min_alarm
:
778 return pvt_read_alarm(pvt
, PVT_VOLT
+ ch
, true, val
);
779 case hwmon_in_max_alarm
:
780 return pvt_read_alarm(pvt
, PVT_VOLT
+ ch
, false, val
);
790 static int pvt_hwmon_read_string(struct device
*dev
,
791 enum hwmon_sensor_types type
,
792 u32 attr
, int ch
, const char **str
)
794 if (!pvt_hwmon_channel_is_valid(type
, ch
))
800 case hwmon_temp_label
:
801 *str
= pvt_info
[ch
].label
;
808 *str
= pvt_info
[PVT_VOLT
+ ch
].label
;
819 static int pvt_hwmon_write(struct device
*dev
, enum hwmon_sensor_types type
,
820 u32 attr
, int ch
, long val
)
822 struct pvt_hwmon
*pvt
= dev_get_drvdata(dev
);
824 if (!pvt_hwmon_channel_is_valid(type
, ch
))
830 case hwmon_chip_update_interval
:
831 return pvt_write_timeout(pvt
, val
);
837 return pvt_write_limit(pvt
, ch
, true, val
);
839 return pvt_write_limit(pvt
, ch
, false, val
);
840 case hwmon_temp_offset
:
841 return pvt_write_trim(pvt
, val
);
847 return pvt_write_limit(pvt
, PVT_VOLT
+ ch
, true, val
);
849 return pvt_write_limit(pvt
, PVT_VOLT
+ ch
, false, val
);
859 static const struct hwmon_ops pvt_hwmon_ops
= {
860 .is_visible
= pvt_hwmon_is_visible
,
861 .read
= pvt_hwmon_read
,
862 .read_string
= pvt_hwmon_read_string
,
863 .write
= pvt_hwmon_write
866 static const struct hwmon_chip_info pvt_hwmon_info
= {
867 .ops
= &pvt_hwmon_ops
,
868 .info
= pvt_channel_info
871 static void pvt_clear_data(void *data
)
873 struct pvt_hwmon
*pvt
= data
;
874 #if !defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
877 for (idx
= 0; idx
< PVT_SENSORS_NUM
; ++idx
)
878 complete_all(&pvt
->cache
[idx
].conversion
);
881 mutex_destroy(&pvt
->iface_mtx
);
884 static struct pvt_hwmon
*pvt_create_data(struct platform_device
*pdev
)
886 struct device
*dev
= &pdev
->dev
;
887 struct pvt_hwmon
*pvt
;
890 pvt
= devm_kzalloc(dev
, sizeof(*pvt
), GFP_KERNEL
);
892 return ERR_PTR(-ENOMEM
);
894 ret
= devm_add_action(dev
, pvt_clear_data
, pvt
);
896 dev_err(dev
, "Can't add PVT data clear action\n");
901 pvt
->sensor
= PVT_SENSOR_FIRST
;
902 mutex_init(&pvt
->iface_mtx
);
904 #if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
905 for (idx
= 0; idx
< PVT_SENSORS_NUM
; ++idx
)
906 seqlock_init(&pvt
->cache
[idx
].data_seqlock
);
908 for (idx
= 0; idx
< PVT_SENSORS_NUM
; ++idx
)
909 init_completion(&pvt
->cache
[idx
].conversion
);
915 static int pvt_request_regs(struct pvt_hwmon
*pvt
)
917 struct platform_device
*pdev
= to_platform_device(pvt
->dev
);
918 struct resource
*res
;
920 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
922 dev_err(pvt
->dev
, "Couldn't find PVT memresource\n");
926 pvt
->regs
= devm_ioremap_resource(pvt
->dev
, res
);
927 if (IS_ERR(pvt
->regs
)) {
928 dev_err(pvt
->dev
, "Couldn't map PVT registers\n");
929 return PTR_ERR(pvt
->regs
);
935 static void pvt_disable_clks(void *data
)
937 struct pvt_hwmon
*pvt
= data
;
939 clk_bulk_disable_unprepare(PVT_CLOCK_NUM
, pvt
->clks
);
942 static int pvt_request_clks(struct pvt_hwmon
*pvt
)
946 pvt
->clks
[PVT_CLOCK_APB
].id
= "pclk";
947 pvt
->clks
[PVT_CLOCK_REF
].id
= "ref";
949 ret
= devm_clk_bulk_get(pvt
->dev
, PVT_CLOCK_NUM
, pvt
->clks
);
951 dev_err(pvt
->dev
, "Couldn't get PVT clocks descriptors\n");
955 ret
= clk_bulk_prepare_enable(PVT_CLOCK_NUM
, pvt
->clks
);
957 dev_err(pvt
->dev
, "Couldn't enable the PVT clocks\n");
961 ret
= devm_add_action_or_reset(pvt
->dev
, pvt_disable_clks
, pvt
);
963 dev_err(pvt
->dev
, "Can't add PVT clocks disable action\n");
970 static int pvt_check_pwr(struct pvt_hwmon
*pvt
)
977 * Test out the sensor conversion functionality. If it is not done on
978 * time then the domain must have been unpowered and we won't be able
979 * to use the device later in this driver.
980 * Note If the power source is lost during the normal driver work the
981 * data read procedure will either return -ETIMEDOUT (for the
982 * alarm-less driver configuration) or just stop the repeated
983 * conversion. In the later case alas we won't be able to detect the
986 pvt_update(pvt
->regs
+ PVT_INTR_MASK
, PVT_INTR_ALL
, PVT_INTR_ALL
);
987 pvt_update(pvt
->regs
+ PVT_CTRL
, PVT_CTRL_EN
, PVT_CTRL_EN
);
988 pvt_set_tout(pvt
, 0);
989 readl(pvt
->regs
+ PVT_DATA
);
991 tout
= PVT_TOUT_MIN
/ NSEC_PER_USEC
;
992 usleep_range(tout
, 2 * tout
);
994 data
= readl(pvt
->regs
+ PVT_DATA
);
995 if (!(data
& PVT_DATA_VALID
)) {
997 dev_err(pvt
->dev
, "Sensor is powered down\n");
1000 pvt_update(pvt
->regs
+ PVT_CTRL
, PVT_CTRL_EN
, 0);
1005 static int pvt_init_iface(struct pvt_hwmon
*pvt
)
1010 rate
= clk_get_rate(pvt
->clks
[PVT_CLOCK_REF
].clk
);
1012 dev_err(pvt
->dev
, "Invalid reference clock rate\n");
1017 * Make sure all interrupts and controller are disabled so not to
1018 * accidentally have ISR executed before the driver data is fully
1019 * initialized. Clear the IRQ status as well.
1021 pvt_update(pvt
->regs
+ PVT_INTR_MASK
, PVT_INTR_ALL
, PVT_INTR_ALL
);
1022 pvt_update(pvt
->regs
+ PVT_CTRL
, PVT_CTRL_EN
, 0);
1023 readl(pvt
->regs
+ PVT_CLR_INTR
);
1024 readl(pvt
->regs
+ PVT_DATA
);
1026 /* Setup default sensor mode, timeout and temperature trim. */
1027 pvt_set_mode(pvt
, pvt_info
[pvt
->sensor
].mode
);
1028 pvt_set_tout(pvt
, PVT_TOUT_DEF
);
1031 * Preserve the current ref-clock based delay (Ttotal) between the
1032 * sensors data samples in the driver data so not to recalculate it
1033 * each time on the data requests and timeout reads. It consists of the
1034 * delay introduced by the internal ref-clock timer (N / Fclk) and the
1035 * constant timeout caused by each conversion latency (Tmin):
1036 * Ttotal = N / Fclk + Tmin
1037 * If alarms are enabled the sensors are polled one after another and
1038 * in order to get the next measurement of a particular sensor the
1039 * caller will have to wait for at most until all the others are
1040 * polled. In that case the formulae will look a bit different:
1041 * Ttotal = 5 * (N / Fclk + Tmin)
1043 #if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
1044 pvt
->timeout
= ktime_set(PVT_SENSORS_NUM
* PVT_TOUT_DEF
, 0);
1045 pvt
->timeout
= ktime_divns(pvt
->timeout
, rate
);
1046 pvt
->timeout
= ktime_add_ns(pvt
->timeout
, PVT_SENSORS_NUM
* PVT_TOUT_MIN
);
1048 pvt
->timeout
= ktime_set(PVT_TOUT_DEF
, 0);
1049 pvt
->timeout
= ktime_divns(pvt
->timeout
, rate
);
1050 pvt
->timeout
= ktime_add_ns(pvt
->timeout
, PVT_TOUT_MIN
);
1053 trim
= PVT_TRIM_DEF
;
1054 if (!of_property_read_u32(pvt
->dev
->of_node
,
1055 "baikal,pvt-temp-offset-millicelsius", &temp
))
1056 trim
= pvt_calc_trim(temp
);
1058 pvt_set_trim(pvt
, trim
);
1063 static int pvt_request_irq(struct pvt_hwmon
*pvt
)
1065 struct platform_device
*pdev
= to_platform_device(pvt
->dev
);
1068 pvt
->irq
= platform_get_irq(pdev
, 0);
1072 ret
= devm_request_threaded_irq(pvt
->dev
, pvt
->irq
,
1073 pvt_hard_isr
, pvt_soft_isr
,
1074 #if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
1075 IRQF_SHARED
| IRQF_TRIGGER_HIGH
|
1078 IRQF_SHARED
| IRQF_TRIGGER_HIGH
,
1082 dev_err(pvt
->dev
, "Couldn't request PVT IRQ\n");
1089 static int pvt_create_hwmon(struct pvt_hwmon
*pvt
)
1091 pvt
->hwmon
= devm_hwmon_device_register_with_info(pvt
->dev
, "pvt", pvt
,
1092 &pvt_hwmon_info
, NULL
);
1093 if (IS_ERR(pvt
->hwmon
)) {
1094 dev_err(pvt
->dev
, "Couldn't create hwmon device\n");
1095 return PTR_ERR(pvt
->hwmon
);
1101 #if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
1103 static void pvt_disable_iface(void *data
)
1105 struct pvt_hwmon
*pvt
= data
;
1107 mutex_lock(&pvt
->iface_mtx
);
1108 pvt_update(pvt
->regs
+ PVT_CTRL
, PVT_CTRL_EN
, 0);
1109 pvt_update(pvt
->regs
+ PVT_INTR_MASK
, PVT_INTR_DVALID
,
1111 mutex_unlock(&pvt
->iface_mtx
);
1114 static int pvt_enable_iface(struct pvt_hwmon
*pvt
)
1118 ret
= devm_add_action(pvt
->dev
, pvt_disable_iface
, pvt
);
1120 dev_err(pvt
->dev
, "Can't add PVT disable interface action\n");
1125 * Enable sensors data conversion and IRQ. We need to lock the
1126 * interface mutex since hwmon has just been created and the
1127 * corresponding sysfs files are accessible from user-space,
1128 * which theoretically may cause races.
1130 mutex_lock(&pvt
->iface_mtx
);
1131 pvt_update(pvt
->regs
+ PVT_INTR_MASK
, PVT_INTR_DVALID
, 0);
1132 pvt_update(pvt
->regs
+ PVT_CTRL
, PVT_CTRL_EN
, PVT_CTRL_EN
);
1133 mutex_unlock(&pvt
->iface_mtx
);
1138 #else /* !CONFIG_SENSORS_BT1_PVT_ALARMS */
1140 static int pvt_enable_iface(struct pvt_hwmon
*pvt
)
1145 #endif /* !CONFIG_SENSORS_BT1_PVT_ALARMS */
1147 static int pvt_probe(struct platform_device
*pdev
)
1149 struct pvt_hwmon
*pvt
;
1152 pvt
= pvt_create_data(pdev
);
1154 return PTR_ERR(pvt
);
1156 ret
= pvt_request_regs(pvt
);
1160 ret
= pvt_request_clks(pvt
);
1164 ret
= pvt_check_pwr(pvt
);
1168 ret
= pvt_init_iface(pvt
);
1172 ret
= pvt_request_irq(pvt
);
1176 ret
= pvt_create_hwmon(pvt
);
1180 ret
= pvt_enable_iface(pvt
);
1187 static const struct of_device_id pvt_of_match
[] = {
1188 { .compatible
= "baikal,bt1-pvt" },
1191 MODULE_DEVICE_TABLE(of
, pvt_of_match
);
1193 static struct platform_driver pvt_driver
= {
1197 .of_match_table
= pvt_of_match
1200 module_platform_driver(pvt_driver
);
1202 MODULE_AUTHOR("Maxim Kaurkin <maxim.kaurkin@baikalelectronics.ru>");
1203 MODULE_DESCRIPTION("Baikal-T1 PVT driver");
1204 MODULE_LICENSE("GPL v2");