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/polynomial.h>
30 #include <linux/seqlock.h>
31 #include <linux/sysfs.h>
32 #include <linux/types.h>
37 * For the sake of the code simplification we created the sensors info table
38 * with the sensor names, activation modes, threshold registers base address
39 * and the thresholds bit fields.
41 static const struct pvt_sensor_info pvt_info[] = {
42 PVT_SENSOR_INFO(0, "CPU Core Temperature", hwmon_temp, TEMP, TTHRES),
43 PVT_SENSOR_INFO(0, "CPU Core Voltage", hwmon_in, VOLT, VTHRES),
44 PVT_SENSOR_INFO(1, "CPU Core Low-Vt", hwmon_in, LVT, LTHRES),
45 PVT_SENSOR_INFO(2, "CPU Core High-Vt", hwmon_in, HVT, HTHRES),
46 PVT_SENSOR_INFO(3, "CPU Core Standard-Vt", hwmon_in, SVT, STHRES),
50 * The original translation formulae of the temperature (in degrees of Celsius)
51 * to PVT data and vice-versa are following:
52 * N = 1.8322e-8*(T^4) + 2.343e-5*(T^3) + 8.7018e-3*(T^2) + 3.9269*(T^1) +
54 * T = -1.6743e-11*(N^4) + 8.1542e-8*(N^3) + -1.8201e-4*(N^2) +
55 * 3.1020e-1*(N^1) - 4.838e1,
56 * where T = [-48.380, 147.438]C and N = [0, 1023].
57 * They must be accordingly altered to be suitable for the integer arithmetics.
58 * The technique is called 'factor redistribution', which just makes sure the
59 * multiplications and divisions are made so to have a result of the operations
60 * within the integer numbers limit. In addition we need to translate the
61 * formulae to accept millidegrees of Celsius. Here what they look like after
63 * N = (18322e-20*(T^4) + 2343e-13*(T^3) + 87018e-9*(T^2) + 39269e-3*T +
65 * T = -16743e-12*(D^4) + 81542e-9*(D^3) - 182010e-6*(D^2) + 310200e-3*D -
67 * where T = [-48380, 147438] mC and N = [0, 1023].
69 static const struct polynomial __maybe_unused poly_temp_to_N = {
70 .total_divider = 10000,
72 {4, 18322, 10000, 10000},
74 {2, 87018, 10000, 10},
80 static const struct polynomial poly_N_to_temp = {
85 {2, -182010, 1000, 1},
92 * Similar alterations are performed for the voltage conversion equations.
93 * The original formulae are:
94 * N = 1.8658e3*V - 1.1572e3,
95 * V = (N + 1.1572e3) / 1.8658e3,
96 * where V = [0.620, 1.168] V and N = [0, 1023].
97 * After the optimization they looks as follows:
98 * N = (18658e-3*V - 11572) / 10,
99 * V = N * 10^5 / 18658 + 11572 * 10^4 / 18658.
101 static const struct polynomial __maybe_unused poly_volt_to_N = {
109 static const struct polynomial poly_N_to_volt = {
112 {1, 100000, 18658, 1},
113 {0, 115720000, 1, 18658}
117 static inline u32 pvt_update(void __iomem *reg, u32 mask, u32 data)
121 old = readl_relaxed(reg);
122 writel((old & ~mask) | (data & mask), reg);
128 * Baikal-T1 PVT mode can be updated only when the controller is disabled.
129 * So first we disable it, then set the new mode together with the controller
130 * getting back enabled. The same concerns the temperature trim and
131 * measurements timeout. If it is necessary the interface mutex is supposed
132 * to be locked at the time the operations are performed.
134 static inline void pvt_set_mode(struct pvt_hwmon *pvt, u32 mode)
138 mode = FIELD_PREP(PVT_CTRL_MODE_MASK, mode);
140 old = pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
141 pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_MODE_MASK | PVT_CTRL_EN,
145 static inline u32 pvt_calc_trim(long temp)
147 temp = clamp_val(temp, 0, PVT_TRIM_TEMP);
149 return DIV_ROUND_UP(temp, PVT_TRIM_STEP);
152 static inline void pvt_set_trim(struct pvt_hwmon *pvt, u32 trim)
156 trim = FIELD_PREP(PVT_CTRL_TRIM_MASK, trim);
158 old = pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
159 pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_TRIM_MASK | PVT_CTRL_EN,
163 static inline void pvt_set_tout(struct pvt_hwmon *pvt, u32 tout)
167 old = pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
168 writel(tout, pvt->regs + PVT_TTIMEOUT);
169 pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, old);
173 * This driver can optionally provide the hwmon alarms for each sensor the PVT
174 * controller supports. The alarms functionality is made compile-time
175 * configurable due to the hardware interface implementation peculiarity
176 * described further in this comment. So in case if alarms are unnecessary in
177 * your system design it's recommended to have them disabled to prevent the PVT
178 * IRQs being periodically raised to get the data cache/alarms status up to
181 * Baikal-T1 PVT embedded controller is based on the Analog Bits PVT sensor,
182 * but is equipped with a dedicated control wrapper. It exposes the PVT
183 * sub-block registers space via the APB3 bus. In addition the wrapper provides
184 * a common interrupt vector of the sensors conversion completion events and
185 * threshold value alarms. Alas the wrapper interface hasn't been fully thought
186 * through. There is only one sensor can be activated at a time, for which the
187 * thresholds comparator is enabled right after the data conversion is
188 * completed. Due to this if alarms need to be implemented for all available
189 * sensors we can't just set the thresholds and enable the interrupts. We need
190 * to enable the sensors one after another and let the controller to detect
191 * the alarms by itself at each conversion. This also makes pointless to handle
192 * the alarms interrupts, since in occasion they happen synchronously with
193 * data conversion completion. The best driver design would be to have the
194 * completion interrupts enabled only and keep the converted value in the
195 * driver data cache. This solution is implemented if hwmon alarms are enabled
196 * in this driver. In case if the alarms are disabled, the conversion is
197 * performed on demand at the time a sensors input file is read.
200 #if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
202 #define pvt_hard_isr NULL
204 static irqreturn_t pvt_soft_isr(int irq, void *data)
206 const struct pvt_sensor_info *info;
207 struct pvt_hwmon *pvt = data;
208 struct pvt_cache *cache;
209 u32 val, thres_sts, old;
212 * DVALID bit will be cleared by reading the data. We need to save the
213 * status before the next conversion happens. Threshold events will be
214 * handled a bit later.
216 thres_sts = readl(pvt->regs + PVT_RAW_INTR_STAT);
219 * Then lets recharge the PVT interface with the next sampling mode.
220 * Lock the interface mutex to serialize trim, timeouts and alarm
221 * thresholds settings.
223 cache = &pvt->cache[pvt->sensor];
224 info = &pvt_info[pvt->sensor];
225 pvt->sensor = (pvt->sensor == PVT_SENSOR_LAST) ?
226 PVT_SENSOR_FIRST : (pvt->sensor + 1);
229 * For some reason we have to mask the interrupt before changing the
230 * mode, otherwise sometimes the temperature mode doesn't get
231 * activated even though the actual mode in the ctrl register
232 * corresponds to one. Then we read the data. By doing so we also
233 * recharge the data conversion. After this the mode corresponding
234 * to the next sensor in the row is set. Finally we enable the
237 mutex_lock(&pvt->iface_mtx);
239 old = pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID,
242 val = readl(pvt->regs + PVT_DATA);
244 pvt_set_mode(pvt, pvt_info[pvt->sensor].mode);
246 pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID, old);
248 mutex_unlock(&pvt->iface_mtx);
251 * We can now update the data cache with data just retrieved from the
252 * sensor. Lock write-seqlock to make sure the reader has a coherent
255 write_seqlock(&cache->data_seqlock);
257 cache->data = FIELD_GET(PVT_DATA_DATA_MASK, val);
259 write_sequnlock(&cache->data_seqlock);
262 * While PVT core is doing the next mode data conversion, we'll check
263 * whether the alarms were triggered for the current sensor. Note that
264 * according to the documentation only one threshold IRQ status can be
265 * set at a time, that's why if-else statement is utilized.
267 if ((thres_sts & info->thres_sts_lo) ^ cache->thres_sts_lo) {
268 WRITE_ONCE(cache->thres_sts_lo, thres_sts & info->thres_sts_lo);
269 hwmon_notify_event(pvt->hwmon, info->type, info->attr_min_alarm,
271 } else if ((thres_sts & info->thres_sts_hi) ^ cache->thres_sts_hi) {
272 WRITE_ONCE(cache->thres_sts_hi, thres_sts & info->thres_sts_hi);
273 hwmon_notify_event(pvt->hwmon, info->type, info->attr_max_alarm,
280 static inline umode_t pvt_limit_is_visible(enum pvt_sensor_type type)
285 static inline umode_t pvt_alarm_is_visible(enum pvt_sensor_type type)
290 static int pvt_read_data(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
293 struct pvt_cache *cache = &pvt->cache[type];
298 seq = read_seqbegin(&cache->data_seqlock);
300 } while (read_seqretry(&cache->data_seqlock, seq));
302 if (type == PVT_TEMP)
303 *val = polynomial_calc(&poly_N_to_temp, data);
305 *val = polynomial_calc(&poly_N_to_volt, data);
310 static int pvt_read_limit(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
311 bool is_low, long *val)
315 /* No need in serialization, since it is just read from MMIO. */
316 data = readl(pvt->regs + pvt_info[type].thres_base);
319 data = FIELD_GET(PVT_THRES_LO_MASK, data);
321 data = FIELD_GET(PVT_THRES_HI_MASK, data);
323 if (type == PVT_TEMP)
324 *val = polynomial_calc(&poly_N_to_temp, data);
326 *val = polynomial_calc(&poly_N_to_volt, data);
331 static int pvt_write_limit(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
332 bool is_low, long val)
334 u32 data, limit, mask;
337 if (type == PVT_TEMP) {
338 val = clamp(val, PVT_TEMP_MIN, PVT_TEMP_MAX);
339 data = polynomial_calc(&poly_temp_to_N, val);
341 val = clamp(val, PVT_VOLT_MIN, PVT_VOLT_MAX);
342 data = polynomial_calc(&poly_volt_to_N, val);
345 /* Serialize limit update, since a part of the register is changed. */
346 ret = mutex_lock_interruptible(&pvt->iface_mtx);
350 /* Make sure the upper and lower ranges don't intersect. */
351 limit = readl(pvt->regs + pvt_info[type].thres_base);
353 limit = FIELD_GET(PVT_THRES_HI_MASK, limit);
354 data = clamp_val(data, PVT_DATA_MIN, limit);
355 data = FIELD_PREP(PVT_THRES_LO_MASK, data);
356 mask = PVT_THRES_LO_MASK;
358 limit = FIELD_GET(PVT_THRES_LO_MASK, limit);
359 data = clamp_val(data, limit, PVT_DATA_MAX);
360 data = FIELD_PREP(PVT_THRES_HI_MASK, data);
361 mask = PVT_THRES_HI_MASK;
364 pvt_update(pvt->regs + pvt_info[type].thres_base, mask, data);
366 mutex_unlock(&pvt->iface_mtx);
371 static int pvt_read_alarm(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
372 bool is_low, long *val)
375 *val = !!READ_ONCE(pvt->cache[type].thres_sts_lo);
377 *val = !!READ_ONCE(pvt->cache[type].thres_sts_hi);
382 static const struct hwmon_channel_info * const pvt_channel_info[] = {
383 HWMON_CHANNEL_INFO(chip,
384 HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL),
385 HWMON_CHANNEL_INFO(temp,
386 HWMON_T_INPUT | HWMON_T_TYPE | HWMON_T_LABEL |
387 HWMON_T_MIN | HWMON_T_MIN_ALARM |
388 HWMON_T_MAX | HWMON_T_MAX_ALARM |
390 HWMON_CHANNEL_INFO(in,
391 HWMON_I_INPUT | HWMON_I_LABEL |
392 HWMON_I_MIN | HWMON_I_MIN_ALARM |
393 HWMON_I_MAX | HWMON_I_MAX_ALARM,
394 HWMON_I_INPUT | HWMON_I_LABEL |
395 HWMON_I_MIN | HWMON_I_MIN_ALARM |
396 HWMON_I_MAX | HWMON_I_MAX_ALARM,
397 HWMON_I_INPUT | HWMON_I_LABEL |
398 HWMON_I_MIN | HWMON_I_MIN_ALARM |
399 HWMON_I_MAX | HWMON_I_MAX_ALARM,
400 HWMON_I_INPUT | HWMON_I_LABEL |
401 HWMON_I_MIN | HWMON_I_MIN_ALARM |
402 HWMON_I_MAX | HWMON_I_MAX_ALARM),
406 #else /* !CONFIG_SENSORS_BT1_PVT_ALARMS */
408 static irqreturn_t pvt_hard_isr(int irq, void *data)
410 struct pvt_hwmon *pvt = data;
411 struct pvt_cache *cache;
415 * Mask the DVALID interrupt so after exiting from the handler a
416 * repeated conversion wouldn't happen.
418 pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID,
422 * Nothing special for alarm-less driver. Just read the data, update
423 * the cache and notify a waiter of this event.
425 val = readl(pvt->regs + PVT_DATA);
426 if (!(val & PVT_DATA_VALID)) {
427 dev_err(pvt->dev, "Got IRQ when data isn't valid\n");
431 cache = &pvt->cache[pvt->sensor];
433 WRITE_ONCE(cache->data, FIELD_GET(PVT_DATA_DATA_MASK, val));
435 complete(&cache->conversion);
440 #define pvt_soft_isr NULL
442 static inline umode_t pvt_limit_is_visible(enum pvt_sensor_type type)
447 static inline umode_t pvt_alarm_is_visible(enum pvt_sensor_type type)
452 static int pvt_read_data(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
455 struct pvt_cache *cache = &pvt->cache[type];
456 unsigned long timeout;
461 * Lock PVT conversion interface until data cache is updated. The
462 * data read procedure is following: set the requested PVT sensor
463 * mode, enable IRQ and conversion, wait until conversion is finished,
464 * then disable conversion and IRQ, and read the cached data.
466 ret = mutex_lock_interruptible(&pvt->iface_mtx);
471 pvt_set_mode(pvt, pvt_info[type].mode);
474 * Unmask the DVALID interrupt and enable the sensors conversions.
475 * Do the reverse procedure when conversion is done.
477 pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID, 0);
478 pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, PVT_CTRL_EN);
481 * Wait with timeout since in case if the sensor is suddenly powered
482 * down the request won't be completed and the caller will hang up on
483 * this procedure until the power is back up again. Multiply the
484 * timeout by the factor of two to prevent a false timeout.
486 timeout = 2 * usecs_to_jiffies(ktime_to_us(pvt->timeout));
487 ret = wait_for_completion_timeout(&cache->conversion, timeout);
489 pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
490 pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID,
493 data = READ_ONCE(cache->data);
495 mutex_unlock(&pvt->iface_mtx);
500 if (type == PVT_TEMP)
501 *val = polynomial_calc(&poly_N_to_temp, data);
503 *val = polynomial_calc(&poly_N_to_volt, data);
508 static int pvt_read_limit(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
509 bool is_low, long *val)
514 static int pvt_write_limit(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
515 bool is_low, long val)
520 static int pvt_read_alarm(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
521 bool is_low, long *val)
526 static const struct hwmon_channel_info * const pvt_channel_info[] = {
527 HWMON_CHANNEL_INFO(chip,
528 HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL),
529 HWMON_CHANNEL_INFO(temp,
530 HWMON_T_INPUT | HWMON_T_TYPE | HWMON_T_LABEL |
532 HWMON_CHANNEL_INFO(in,
533 HWMON_I_INPUT | HWMON_I_LABEL,
534 HWMON_I_INPUT | HWMON_I_LABEL,
535 HWMON_I_INPUT | HWMON_I_LABEL,
536 HWMON_I_INPUT | HWMON_I_LABEL),
540 #endif /* !CONFIG_SENSORS_BT1_PVT_ALARMS */
542 static inline bool pvt_hwmon_channel_is_valid(enum hwmon_sensor_types type,
547 if (ch < 0 || ch >= PVT_TEMP_CHS)
551 if (ch < 0 || ch >= PVT_VOLT_CHS)
558 /* The rest of the types are independent from the channel number. */
562 static umode_t pvt_hwmon_is_visible(const void *data,
563 enum hwmon_sensor_types type,
566 if (!pvt_hwmon_channel_is_valid(type, ch))
572 case hwmon_chip_update_interval:
578 case hwmon_temp_input:
579 case hwmon_temp_type:
580 case hwmon_temp_label:
584 return pvt_limit_is_visible(ch);
585 case hwmon_temp_min_alarm:
586 case hwmon_temp_max_alarm:
587 return pvt_alarm_is_visible(ch);
588 case hwmon_temp_offset:
599 return pvt_limit_is_visible(PVT_VOLT + ch);
600 case hwmon_in_min_alarm:
601 case hwmon_in_max_alarm:
602 return pvt_alarm_is_visible(PVT_VOLT + ch);
612 static int pvt_read_trim(struct pvt_hwmon *pvt, long *val)
616 data = readl(pvt->regs + PVT_CTRL);
617 *val = FIELD_GET(PVT_CTRL_TRIM_MASK, data) * PVT_TRIM_STEP;
622 static int pvt_write_trim(struct pvt_hwmon *pvt, long val)
628 * Serialize trim update, since a part of the register is changed and
629 * the controller is supposed to be disabled during this operation.
631 ret = mutex_lock_interruptible(&pvt->iface_mtx);
635 trim = pvt_calc_trim(val);
636 pvt_set_trim(pvt, trim);
638 mutex_unlock(&pvt->iface_mtx);
643 static int pvt_read_timeout(struct pvt_hwmon *pvt, long *val)
647 ret = mutex_lock_interruptible(&pvt->iface_mtx);
651 /* Return the result in msec as hwmon sysfs interface requires. */
652 *val = ktime_to_ms(pvt->timeout);
654 mutex_unlock(&pvt->iface_mtx);
659 static int pvt_write_timeout(struct pvt_hwmon *pvt, long val)
666 rate = clk_get_rate(pvt->clks[PVT_CLOCK_REF].clk);
671 * If alarms are enabled, the requested timeout must be divided
672 * between all available sensors to have the requested delay
673 * applicable to each individual sensor.
675 cache = kt = ms_to_ktime(val);
676 #if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
677 kt = ktime_divns(kt, PVT_SENSORS_NUM);
681 * Subtract a constant lag, which always persists due to the limited
682 * PVT sampling rate. Make sure the timeout is not negative.
684 kt = ktime_sub_ns(kt, PVT_TOUT_MIN);
685 if (ktime_to_ns(kt) < 0)
686 kt = ktime_set(0, 0);
689 * Finally recalculate the timeout in terms of the reference clock
692 data = ktime_divns(kt * rate, NSEC_PER_SEC);
695 * Update the measurements delay, but lock the interface first, since
696 * we have to disable PVT in order to have the new delay actually
699 ret = mutex_lock_interruptible(&pvt->iface_mtx);
703 pvt_set_tout(pvt, data);
704 pvt->timeout = cache;
706 mutex_unlock(&pvt->iface_mtx);
711 static int pvt_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
712 u32 attr, int ch, long *val)
714 struct pvt_hwmon *pvt = dev_get_drvdata(dev);
716 if (!pvt_hwmon_channel_is_valid(type, ch))
722 case hwmon_chip_update_interval:
723 return pvt_read_timeout(pvt, val);
728 case hwmon_temp_input:
729 return pvt_read_data(pvt, ch, val);
730 case hwmon_temp_type:
734 return pvt_read_limit(pvt, ch, true, val);
736 return pvt_read_limit(pvt, ch, false, val);
737 case hwmon_temp_min_alarm:
738 return pvt_read_alarm(pvt, ch, true, val);
739 case hwmon_temp_max_alarm:
740 return pvt_read_alarm(pvt, ch, false, val);
741 case hwmon_temp_offset:
742 return pvt_read_trim(pvt, val);
748 return pvt_read_data(pvt, PVT_VOLT + ch, val);
750 return pvt_read_limit(pvt, PVT_VOLT + ch, true, val);
752 return pvt_read_limit(pvt, PVT_VOLT + ch, false, val);
753 case hwmon_in_min_alarm:
754 return pvt_read_alarm(pvt, PVT_VOLT + ch, true, val);
755 case hwmon_in_max_alarm:
756 return pvt_read_alarm(pvt, PVT_VOLT + ch, false, val);
766 static int pvt_hwmon_read_string(struct device *dev,
767 enum hwmon_sensor_types type,
768 u32 attr, int ch, const char **str)
770 if (!pvt_hwmon_channel_is_valid(type, ch))
776 case hwmon_temp_label:
777 *str = pvt_info[ch].label;
784 *str = pvt_info[PVT_VOLT + ch].label;
795 static int pvt_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
796 u32 attr, int ch, long val)
798 struct pvt_hwmon *pvt = dev_get_drvdata(dev);
800 if (!pvt_hwmon_channel_is_valid(type, ch))
806 case hwmon_chip_update_interval:
807 return pvt_write_timeout(pvt, val);
813 return pvt_write_limit(pvt, ch, true, val);
815 return pvt_write_limit(pvt, ch, false, val);
816 case hwmon_temp_offset:
817 return pvt_write_trim(pvt, val);
823 return pvt_write_limit(pvt, PVT_VOLT + ch, true, val);
825 return pvt_write_limit(pvt, PVT_VOLT + ch, false, val);
835 static const struct hwmon_ops pvt_hwmon_ops = {
836 .is_visible = pvt_hwmon_is_visible,
837 .read = pvt_hwmon_read,
838 .read_string = pvt_hwmon_read_string,
839 .write = pvt_hwmon_write
842 static const struct hwmon_chip_info pvt_hwmon_info = {
843 .ops = &pvt_hwmon_ops,
844 .info = pvt_channel_info
847 static void pvt_clear_data(void *data)
849 struct pvt_hwmon *pvt = data;
850 #if !defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
853 for (idx = 0; idx < PVT_SENSORS_NUM; ++idx)
854 complete_all(&pvt->cache[idx].conversion);
857 mutex_destroy(&pvt->iface_mtx);
860 static struct pvt_hwmon *pvt_create_data(struct platform_device *pdev)
862 struct device *dev = &pdev->dev;
863 struct pvt_hwmon *pvt;
866 pvt = devm_kzalloc(dev, sizeof(*pvt), GFP_KERNEL);
868 return ERR_PTR(-ENOMEM);
870 ret = devm_add_action(dev, pvt_clear_data, pvt);
872 dev_err(dev, "Can't add PVT data clear action\n");
877 pvt->sensor = PVT_SENSOR_FIRST;
878 mutex_init(&pvt->iface_mtx);
880 #if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
881 for (idx = 0; idx < PVT_SENSORS_NUM; ++idx)
882 seqlock_init(&pvt->cache[idx].data_seqlock);
884 for (idx = 0; idx < PVT_SENSORS_NUM; ++idx)
885 init_completion(&pvt->cache[idx].conversion);
891 static int pvt_request_regs(struct pvt_hwmon *pvt)
893 struct platform_device *pdev = to_platform_device(pvt->dev);
895 pvt->regs = devm_platform_ioremap_resource(pdev, 0);
896 if (IS_ERR(pvt->regs))
897 return PTR_ERR(pvt->regs);
902 static void pvt_disable_clks(void *data)
904 struct pvt_hwmon *pvt = data;
906 clk_bulk_disable_unprepare(PVT_CLOCK_NUM, pvt->clks);
909 static int pvt_request_clks(struct pvt_hwmon *pvt)
913 pvt->clks[PVT_CLOCK_APB].id = "pclk";
914 pvt->clks[PVT_CLOCK_REF].id = "ref";
916 ret = devm_clk_bulk_get(pvt->dev, PVT_CLOCK_NUM, pvt->clks);
918 dev_err(pvt->dev, "Couldn't get PVT clocks descriptors\n");
922 ret = clk_bulk_prepare_enable(PVT_CLOCK_NUM, pvt->clks);
924 dev_err(pvt->dev, "Couldn't enable the PVT clocks\n");
928 ret = devm_add_action_or_reset(pvt->dev, pvt_disable_clks, pvt);
930 dev_err(pvt->dev, "Can't add PVT clocks disable action\n");
937 static int pvt_check_pwr(struct pvt_hwmon *pvt)
944 * Test out the sensor conversion functionality. If it is not done on
945 * time then the domain must have been unpowered and we won't be able
946 * to use the device later in this driver.
947 * Note If the power source is lost during the normal driver work the
948 * data read procedure will either return -ETIMEDOUT (for the
949 * alarm-less driver configuration) or just stop the repeated
950 * conversion. In the later case alas we won't be able to detect the
953 pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_ALL, PVT_INTR_ALL);
954 pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, PVT_CTRL_EN);
955 pvt_set_tout(pvt, 0);
956 readl(pvt->regs + PVT_DATA);
958 tout = PVT_TOUT_MIN / NSEC_PER_USEC;
959 usleep_range(tout, 2 * tout);
961 data = readl(pvt->regs + PVT_DATA);
962 if (!(data & PVT_DATA_VALID)) {
964 dev_err(pvt->dev, "Sensor is powered down\n");
967 pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
972 static int pvt_init_iface(struct pvt_hwmon *pvt)
977 rate = clk_get_rate(pvt->clks[PVT_CLOCK_REF].clk);
979 dev_err(pvt->dev, "Invalid reference clock rate\n");
984 * Make sure all interrupts and controller are disabled so not to
985 * accidentally have ISR executed before the driver data is fully
986 * initialized. Clear the IRQ status as well.
988 pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_ALL, PVT_INTR_ALL);
989 pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
990 readl(pvt->regs + PVT_CLR_INTR);
991 readl(pvt->regs + PVT_DATA);
993 /* Setup default sensor mode, timeout and temperature trim. */
994 pvt_set_mode(pvt, pvt_info[pvt->sensor].mode);
995 pvt_set_tout(pvt, PVT_TOUT_DEF);
998 * Preserve the current ref-clock based delay (Ttotal) between the
999 * sensors data samples in the driver data so not to recalculate it
1000 * each time on the data requests and timeout reads. It consists of the
1001 * delay introduced by the internal ref-clock timer (N / Fclk) and the
1002 * constant timeout caused by each conversion latency (Tmin):
1003 * Ttotal = N / Fclk + Tmin
1004 * If alarms are enabled the sensors are polled one after another and
1005 * in order to get the next measurement of a particular sensor the
1006 * caller will have to wait for at most until all the others are
1007 * polled. In that case the formulae will look a bit different:
1008 * Ttotal = 5 * (N / Fclk + Tmin)
1010 #if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
1011 pvt->timeout = ktime_set(PVT_SENSORS_NUM * PVT_TOUT_DEF, 0);
1012 pvt->timeout = ktime_divns(pvt->timeout, rate);
1013 pvt->timeout = ktime_add_ns(pvt->timeout, PVT_SENSORS_NUM * PVT_TOUT_MIN);
1015 pvt->timeout = ktime_set(PVT_TOUT_DEF, 0);
1016 pvt->timeout = ktime_divns(pvt->timeout, rate);
1017 pvt->timeout = ktime_add_ns(pvt->timeout, PVT_TOUT_MIN);
1020 trim = PVT_TRIM_DEF;
1021 if (!of_property_read_u32(pvt->dev->of_node,
1022 "baikal,pvt-temp-offset-millicelsius", &temp))
1023 trim = pvt_calc_trim(temp);
1025 pvt_set_trim(pvt, trim);
1030 static int pvt_request_irq(struct pvt_hwmon *pvt)
1032 struct platform_device *pdev = to_platform_device(pvt->dev);
1035 pvt->irq = platform_get_irq(pdev, 0);
1039 ret = devm_request_threaded_irq(pvt->dev, pvt->irq,
1040 pvt_hard_isr, pvt_soft_isr,
1041 #if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
1042 IRQF_SHARED | IRQF_TRIGGER_HIGH |
1045 IRQF_SHARED | IRQF_TRIGGER_HIGH,
1049 dev_err(pvt->dev, "Couldn't request PVT IRQ\n");
1056 static int pvt_create_hwmon(struct pvt_hwmon *pvt)
1058 pvt->hwmon = devm_hwmon_device_register_with_info(pvt->dev, "pvt", pvt,
1059 &pvt_hwmon_info, NULL);
1060 if (IS_ERR(pvt->hwmon)) {
1061 dev_err(pvt->dev, "Couldn't create hwmon device\n");
1062 return PTR_ERR(pvt->hwmon);
1068 #if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
1070 static void pvt_disable_iface(void *data)
1072 struct pvt_hwmon *pvt = data;
1074 mutex_lock(&pvt->iface_mtx);
1075 pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
1076 pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID,
1078 mutex_unlock(&pvt->iface_mtx);
1081 static int pvt_enable_iface(struct pvt_hwmon *pvt)
1085 ret = devm_add_action(pvt->dev, pvt_disable_iface, pvt);
1087 dev_err(pvt->dev, "Can't add PVT disable interface action\n");
1092 * Enable sensors data conversion and IRQ. We need to lock the
1093 * interface mutex since hwmon has just been created and the
1094 * corresponding sysfs files are accessible from user-space,
1095 * which theoretically may cause races.
1097 mutex_lock(&pvt->iface_mtx);
1098 pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID, 0);
1099 pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, PVT_CTRL_EN);
1100 mutex_unlock(&pvt->iface_mtx);
1105 #else /* !CONFIG_SENSORS_BT1_PVT_ALARMS */
1107 static int pvt_enable_iface(struct pvt_hwmon *pvt)
1112 #endif /* !CONFIG_SENSORS_BT1_PVT_ALARMS */
1114 static int pvt_probe(struct platform_device *pdev)
1116 struct pvt_hwmon *pvt;
1119 pvt = pvt_create_data(pdev);
1121 return PTR_ERR(pvt);
1123 ret = pvt_request_regs(pvt);
1127 ret = pvt_request_clks(pvt);
1131 ret = pvt_check_pwr(pvt);
1135 ret = pvt_init_iface(pvt);
1139 ret = pvt_request_irq(pvt);
1143 ret = pvt_create_hwmon(pvt);
1147 ret = pvt_enable_iface(pvt);
1154 static const struct of_device_id pvt_of_match[] = {
1155 { .compatible = "baikal,bt1-pvt" },
1158 MODULE_DEVICE_TABLE(of, pvt_of_match);
1160 static struct platform_driver pvt_driver = {
1164 .of_match_table = pvt_of_match
1167 module_platform_driver(pvt_driver);
1169 MODULE_AUTHOR("Maxim Kaurkin <maxim.kaurkin@baikalelectronics.ru>");
1170 MODULE_DESCRIPTION("Baikal-T1 PVT driver");
1171 MODULE_LICENSE("GPL v2");