1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2020 BAIKAL ELECTRONICS, JSC
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/device.h>
17 #include <linux/hwmon-sysfs.h>
18 #include <linux/hwmon.h>
19 #include <linux/interrupt.h>
21 #include <linux/kernel.h>
22 #include <linux/ktime.h>
23 #include <linux/limits.h>
24 #include <linux/module.h>
25 #include <linux/mutex.h>
27 #include <linux/platform_device.h>
28 #include <linux/seqlock.h>
29 #include <linux/sysfs.h>
30 #include <linux/types.h>
35 * For the sake of the code simplification we created the sensors info table
36 * with the sensor names, activation modes, threshold registers base address
37 * and the thresholds bit fields.
39 static const struct pvt_sensor_info pvt_info[] = {
40 PVT_SENSOR_INFO(0, "CPU Core Temperature", hwmon_temp, TEMP, TTHRES),
41 PVT_SENSOR_INFO(0, "CPU Core Voltage", hwmon_in, VOLT, VTHRES),
42 PVT_SENSOR_INFO(1, "CPU Core Low-Vt", hwmon_in, LVT, LTHRES),
43 PVT_SENSOR_INFO(2, "CPU Core High-Vt", hwmon_in, HVT, HTHRES),
44 PVT_SENSOR_INFO(3, "CPU Core Standard-Vt", hwmon_in, SVT, STHRES),
48 * The original translation formulae of the temperature (in degrees of Celsius)
49 * to PVT data and vice-versa are following:
50 * N = 1.8322e-8*(T^4) + 2.343e-5*(T^3) + 8.7018e-3*(T^2) + 3.9269*(T^1) +
52 * T = -1.6743e-11*(N^4) + 8.1542e-8*(N^3) + -1.8201e-4*(N^2) +
53 * 3.1020e-1*(N^1) - 4.838e1,
54 * where T = [-48.380, 147.438]C and N = [0, 1023].
55 * They must be accordingly altered to be suitable for the integer arithmetics.
56 * The technique is called 'factor redistribution', which just makes sure the
57 * multiplications and divisions are made so to have a result of the operations
58 * within the integer numbers limit. In addition we need to translate the
59 * formulae to accept millidegrees of Celsius. Here what they look like after
61 * N = (18322e-20*(T^4) + 2343e-13*(T^3) + 87018e-9*(T^2) + 39269e-3*T +
63 * T = -16743e-12*(D^4) + 81542e-9*(D^3) - 182010e-6*(D^2) + 310200e-3*D -
65 * where T = [-48380, 147438] mC and N = [0, 1023].
67 static const struct pvt_poly __maybe_unused poly_temp_to_N = {
68 .total_divider = 10000,
70 {4, 18322, 10000, 10000},
72 {2, 87018, 10000, 10},
78 static const struct pvt_poly poly_N_to_temp = {
83 {2, -182010, 1000, 1},
90 * Similar alterations are performed for the voltage conversion equations.
91 * The original formulae are:
92 * N = 1.8658e3*V - 1.1572e3,
93 * V = (N + 1.1572e3) / 1.8658e3,
94 * where V = [0.620, 1.168] V and N = [0, 1023].
95 * After the optimization they looks as follows:
96 * N = (18658e-3*V - 11572) / 10,
97 * V = N * 10^5 / 18658 + 11572 * 10^4 / 18658.
99 static const struct pvt_poly __maybe_unused poly_volt_to_N = {
107 static const struct pvt_poly poly_N_to_volt = {
110 {1, 100000, 18658, 1},
111 {0, 115720000, 1, 18658}
116 * Here is the polynomial calculation function, which performs the
117 * redistributed terms calculations. It's pretty straightforward. We walk
118 * over each degree term up to the free one, and perform the redistributed
119 * multiplication of the term coefficient, its divider (as for the rationale
120 * fraction representation), data power and the rational fraction divider
121 * leftover. Then all of this is collected in a total sum variable, which
122 * value is normalized by the total divider before being returned.
124 static long pvt_calc_poly(const struct pvt_poly *poly, long data)
126 const struct pvt_poly_term *term = poly->terms;
132 for (deg = 0; deg < term->deg; ++deg)
133 tmp = mult_frac(tmp, data, term->divider);
134 ret += tmp / term->divider_leftover;
135 } while ((term++)->deg);
137 return ret / poly->total_divider;
140 static inline u32 pvt_update(void __iomem *reg, u32 mask, u32 data)
144 old = readl_relaxed(reg);
145 writel((old & ~mask) | (data & mask), reg);
151 * Baikal-T1 PVT mode can be updated only when the controller is disabled.
152 * So first we disable it, then set the new mode together with the controller
153 * getting back enabled. The same concerns the temperature trim and
154 * measurements timeout. If it is necessary the interface mutex is supposed
155 * to be locked at the time the operations are performed.
157 static inline void pvt_set_mode(struct pvt_hwmon *pvt, u32 mode)
161 mode = FIELD_PREP(PVT_CTRL_MODE_MASK, mode);
163 old = pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
164 pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_MODE_MASK | PVT_CTRL_EN,
168 static inline u32 pvt_calc_trim(long temp)
170 temp = clamp_val(temp, 0, PVT_TRIM_TEMP);
172 return DIV_ROUND_UP(temp, PVT_TRIM_STEP);
175 static inline void pvt_set_trim(struct pvt_hwmon *pvt, u32 trim)
179 trim = FIELD_PREP(PVT_CTRL_TRIM_MASK, trim);
181 old = pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
182 pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_TRIM_MASK | PVT_CTRL_EN,
186 static inline void pvt_set_tout(struct pvt_hwmon *pvt, u32 tout)
190 old = pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
191 writel(tout, pvt->regs + PVT_TTIMEOUT);
192 pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, old);
196 * This driver can optionally provide the hwmon alarms for each sensor the PVT
197 * controller supports. The alarms functionality is made compile-time
198 * configurable due to the hardware interface implementation peculiarity
199 * described further in this comment. So in case if alarms are unnecessary in
200 * your system design it's recommended to have them disabled to prevent the PVT
201 * IRQs being periodically raised to get the data cache/alarms status up to
204 * Baikal-T1 PVT embedded controller is based on the Analog Bits PVT sensor,
205 * but is equipped with a dedicated control wrapper. It exposes the PVT
206 * sub-block registers space via the APB3 bus. In addition the wrapper provides
207 * a common interrupt vector of the sensors conversion completion events and
208 * threshold value alarms. Alas the wrapper interface hasn't been fully thought
209 * through. There is only one sensor can be activated at a time, for which the
210 * thresholds comparator is enabled right after the data conversion is
211 * completed. Due to this if alarms need to be implemented for all available
212 * sensors we can't just set the thresholds and enable the interrupts. We need
213 * to enable the sensors one after another and let the controller to detect
214 * the alarms by itself at each conversion. This also makes pointless to handle
215 * the alarms interrupts, since in occasion they happen synchronously with
216 * data conversion completion. The best driver design would be to have the
217 * completion interrupts enabled only and keep the converted value in the
218 * driver data cache. This solution is implemented if hwmon alarms are enabled
219 * in this driver. In case if the alarms are disabled, the conversion is
220 * performed on demand at the time a sensors input file is read.
223 #if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
225 #define pvt_hard_isr NULL
227 static irqreturn_t pvt_soft_isr(int irq, void *data)
229 const struct pvt_sensor_info *info;
230 struct pvt_hwmon *pvt = data;
231 struct pvt_cache *cache;
232 u32 val, thres_sts, old;
235 * DVALID bit will be cleared by reading the data. We need to save the
236 * status before the next conversion happens. Threshold events will be
237 * handled a bit later.
239 thres_sts = readl(pvt->regs + PVT_RAW_INTR_STAT);
242 * Then lets recharge the PVT interface with the next sampling mode.
243 * Lock the interface mutex to serialize trim, timeouts and alarm
244 * thresholds settings.
246 cache = &pvt->cache[pvt->sensor];
247 info = &pvt_info[pvt->sensor];
248 pvt->sensor = (pvt->sensor == PVT_SENSOR_LAST) ?
249 PVT_SENSOR_FIRST : (pvt->sensor + 1);
252 * For some reason we have to mask the interrupt before changing the
253 * mode, otherwise sometimes the temperature mode doesn't get
254 * activated even though the actual mode in the ctrl register
255 * corresponds to one. Then we read the data. By doing so we also
256 * recharge the data conversion. After this the mode corresponding
257 * to the next sensor in the row is set. Finally we enable the
260 mutex_lock(&pvt->iface_mtx);
262 old = pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID,
265 val = readl(pvt->regs + PVT_DATA);
267 pvt_set_mode(pvt, pvt_info[pvt->sensor].mode);
269 pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID, old);
271 mutex_unlock(&pvt->iface_mtx);
274 * We can now update the data cache with data just retrieved from the
275 * sensor. Lock write-seqlock to make sure the reader has a coherent
278 write_seqlock(&cache->data_seqlock);
280 cache->data = FIELD_GET(PVT_DATA_DATA_MASK, val);
282 write_sequnlock(&cache->data_seqlock);
285 * While PVT core is doing the next mode data conversion, we'll check
286 * whether the alarms were triggered for the current sensor. Note that
287 * according to the documentation only one threshold IRQ status can be
288 * set at a time, that's why if-else statement is utilized.
290 if ((thres_sts & info->thres_sts_lo) ^ cache->thres_sts_lo) {
291 WRITE_ONCE(cache->thres_sts_lo, thres_sts & info->thres_sts_lo);
292 hwmon_notify_event(pvt->hwmon, info->type, info->attr_min_alarm,
294 } else if ((thres_sts & info->thres_sts_hi) ^ cache->thres_sts_hi) {
295 WRITE_ONCE(cache->thres_sts_hi, thres_sts & info->thres_sts_hi);
296 hwmon_notify_event(pvt->hwmon, info->type, info->attr_max_alarm,
303 static inline umode_t pvt_limit_is_visible(enum pvt_sensor_type type)
308 static inline umode_t pvt_alarm_is_visible(enum pvt_sensor_type type)
313 static int pvt_read_data(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
316 struct pvt_cache *cache = &pvt->cache[type];
321 seq = read_seqbegin(&cache->data_seqlock);
323 } while (read_seqretry(&cache->data_seqlock, seq));
325 if (type == PVT_TEMP)
326 *val = pvt_calc_poly(&poly_N_to_temp, data);
328 *val = pvt_calc_poly(&poly_N_to_volt, data);
333 static int pvt_read_limit(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
334 bool is_low, long *val)
338 /* No need in serialization, since it is just read from MMIO. */
339 data = readl(pvt->regs + pvt_info[type].thres_base);
342 data = FIELD_GET(PVT_THRES_LO_MASK, data);
344 data = FIELD_GET(PVT_THRES_HI_MASK, data);
346 if (type == PVT_TEMP)
347 *val = pvt_calc_poly(&poly_N_to_temp, data);
349 *val = pvt_calc_poly(&poly_N_to_volt, data);
354 static int pvt_write_limit(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
355 bool is_low, long val)
357 u32 data, limit, mask;
360 if (type == PVT_TEMP) {
361 val = clamp(val, PVT_TEMP_MIN, PVT_TEMP_MAX);
362 data = pvt_calc_poly(&poly_temp_to_N, val);
364 val = clamp(val, PVT_VOLT_MIN, PVT_VOLT_MAX);
365 data = pvt_calc_poly(&poly_volt_to_N, val);
368 /* Serialize limit update, since a part of the register is changed. */
369 ret = mutex_lock_interruptible(&pvt->iface_mtx);
373 /* Make sure the upper and lower ranges don't intersect. */
374 limit = readl(pvt->regs + pvt_info[type].thres_base);
376 limit = FIELD_GET(PVT_THRES_HI_MASK, limit);
377 data = clamp_val(data, PVT_DATA_MIN, limit);
378 data = FIELD_PREP(PVT_THRES_LO_MASK, data);
379 mask = PVT_THRES_LO_MASK;
381 limit = FIELD_GET(PVT_THRES_LO_MASK, limit);
382 data = clamp_val(data, limit, PVT_DATA_MAX);
383 data = FIELD_PREP(PVT_THRES_HI_MASK, data);
384 mask = PVT_THRES_HI_MASK;
387 pvt_update(pvt->regs + pvt_info[type].thres_base, mask, data);
389 mutex_unlock(&pvt->iface_mtx);
394 static int pvt_read_alarm(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
395 bool is_low, long *val)
398 *val = !!READ_ONCE(pvt->cache[type].thres_sts_lo);
400 *val = !!READ_ONCE(pvt->cache[type].thres_sts_hi);
405 static const struct hwmon_channel_info *pvt_channel_info[] = {
406 HWMON_CHANNEL_INFO(chip,
407 HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL),
408 HWMON_CHANNEL_INFO(temp,
409 HWMON_T_INPUT | HWMON_T_TYPE | HWMON_T_LABEL |
410 HWMON_T_MIN | HWMON_T_MIN_ALARM |
411 HWMON_T_MAX | HWMON_T_MAX_ALARM |
413 HWMON_CHANNEL_INFO(in,
414 HWMON_I_INPUT | HWMON_I_LABEL |
415 HWMON_I_MIN | HWMON_I_MIN_ALARM |
416 HWMON_I_MAX | HWMON_I_MAX_ALARM,
417 HWMON_I_INPUT | HWMON_I_LABEL |
418 HWMON_I_MIN | HWMON_I_MIN_ALARM |
419 HWMON_I_MAX | HWMON_I_MAX_ALARM,
420 HWMON_I_INPUT | HWMON_I_LABEL |
421 HWMON_I_MIN | HWMON_I_MIN_ALARM |
422 HWMON_I_MAX | HWMON_I_MAX_ALARM,
423 HWMON_I_INPUT | HWMON_I_LABEL |
424 HWMON_I_MIN | HWMON_I_MIN_ALARM |
425 HWMON_I_MAX | HWMON_I_MAX_ALARM),
429 #else /* !CONFIG_SENSORS_BT1_PVT_ALARMS */
431 static irqreturn_t pvt_hard_isr(int irq, void *data)
433 struct pvt_hwmon *pvt = data;
434 struct pvt_cache *cache;
438 * Mask the DVALID interrupt so after exiting from the handler a
439 * repeated conversion wouldn't happen.
441 pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID,
445 * Nothing special for alarm-less driver. Just read the data, update
446 * the cache and notify a waiter of this event.
448 val = readl(pvt->regs + PVT_DATA);
449 if (!(val & PVT_DATA_VALID)) {
450 dev_err(pvt->dev, "Got IRQ when data isn't valid\n");
454 cache = &pvt->cache[pvt->sensor];
456 WRITE_ONCE(cache->data, FIELD_GET(PVT_DATA_DATA_MASK, val));
458 complete(&cache->conversion);
463 #define pvt_soft_isr NULL
465 static inline umode_t pvt_limit_is_visible(enum pvt_sensor_type type)
470 static inline umode_t pvt_alarm_is_visible(enum pvt_sensor_type type)
475 static int pvt_read_data(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
478 struct pvt_cache *cache = &pvt->cache[type];
483 * Lock PVT conversion interface until data cache is updated. The
484 * data read procedure is following: set the requested PVT sensor
485 * mode, enable IRQ and conversion, wait until conversion is finished,
486 * then disable conversion and IRQ, and read the cached data.
488 ret = mutex_lock_interruptible(&pvt->iface_mtx);
493 pvt_set_mode(pvt, pvt_info[type].mode);
496 * Unmask the DVALID interrupt and enable the sensors conversions.
497 * Do the reverse procedure when conversion is done.
499 pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID, 0);
500 pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, PVT_CTRL_EN);
502 wait_for_completion(&cache->conversion);
504 pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
505 pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID,
508 data = READ_ONCE(cache->data);
510 mutex_unlock(&pvt->iface_mtx);
512 if (type == PVT_TEMP)
513 *val = pvt_calc_poly(&poly_N_to_temp, data);
515 *val = pvt_calc_poly(&poly_N_to_volt, data);
520 static int pvt_read_limit(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
521 bool is_low, long *val)
526 static int pvt_write_limit(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
527 bool is_low, long val)
532 static int pvt_read_alarm(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
533 bool is_low, long *val)
538 static const struct hwmon_channel_info *pvt_channel_info[] = {
539 HWMON_CHANNEL_INFO(chip,
540 HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL),
541 HWMON_CHANNEL_INFO(temp,
542 HWMON_T_INPUT | HWMON_T_TYPE | HWMON_T_LABEL |
544 HWMON_CHANNEL_INFO(in,
545 HWMON_I_INPUT | HWMON_I_LABEL,
546 HWMON_I_INPUT | HWMON_I_LABEL,
547 HWMON_I_INPUT | HWMON_I_LABEL,
548 HWMON_I_INPUT | HWMON_I_LABEL),
552 #endif /* !CONFIG_SENSORS_BT1_PVT_ALARMS */
554 static inline bool pvt_hwmon_channel_is_valid(enum hwmon_sensor_types type,
559 if (ch < 0 || ch >= PVT_TEMP_CHS)
563 if (ch < 0 || ch >= PVT_VOLT_CHS)
570 /* The rest of the types are independent from the channel number. */
574 static umode_t pvt_hwmon_is_visible(const void *data,
575 enum hwmon_sensor_types type,
578 if (!pvt_hwmon_channel_is_valid(type, ch))
584 case hwmon_chip_update_interval:
590 case hwmon_temp_input:
591 case hwmon_temp_type:
592 case hwmon_temp_label:
596 return pvt_limit_is_visible(ch);
597 case hwmon_temp_min_alarm:
598 case hwmon_temp_max_alarm:
599 return pvt_alarm_is_visible(ch);
600 case hwmon_temp_offset:
611 return pvt_limit_is_visible(PVT_VOLT + ch);
612 case hwmon_in_min_alarm:
613 case hwmon_in_max_alarm:
614 return pvt_alarm_is_visible(PVT_VOLT + ch);
624 static int pvt_read_trim(struct pvt_hwmon *pvt, long *val)
628 data = readl(pvt->regs + PVT_CTRL);
629 *val = FIELD_GET(PVT_CTRL_TRIM_MASK, data) * PVT_TRIM_STEP;
634 static int pvt_write_trim(struct pvt_hwmon *pvt, long val)
640 * Serialize trim update, since a part of the register is changed and
641 * the controller is supposed to be disabled during this operation.
643 ret = mutex_lock_interruptible(&pvt->iface_mtx);
647 trim = pvt_calc_trim(val);
648 pvt_set_trim(pvt, trim);
650 mutex_unlock(&pvt->iface_mtx);
655 static int pvt_read_timeout(struct pvt_hwmon *pvt, long *val)
661 rate = clk_get_rate(pvt->clks[PVT_CLOCK_REF].clk);
666 * Don't bother with mutex here, since we just read data from MMIO.
667 * We also have to scale the ticks timeout up to compensate the
668 * ms-ns-data translations.
670 data = readl(pvt->regs + PVT_TTIMEOUT) + 1;
673 * Calculate ref-clock based delay (Ttotal) between two consecutive
674 * data samples of the same sensor. So we first must calculate the
675 * delay introduced by the internal ref-clock timer (Tref * Fclk).
676 * Then add the constant timeout cuased by each conversion latency
677 * (Tmin). The basic formulae for each conversion is following:
678 * Ttotal = Tref * Fclk + Tmin
679 * Note if alarms are enabled the sensors are polled one after
680 * another, so in order to have the delay being applicable for each
681 * sensor the requested value must be equally redistirbuted.
683 #if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
684 kt = ktime_set(PVT_SENSORS_NUM * (u64)data, 0);
685 kt = ktime_divns(kt, rate);
686 kt = ktime_add_ns(kt, PVT_SENSORS_NUM * PVT_TOUT_MIN);
688 kt = ktime_set(data, 0);
689 kt = ktime_divns(kt, rate);
690 kt = ktime_add_ns(kt, PVT_TOUT_MIN);
693 /* Return the result in msec as hwmon sysfs interface requires. */
694 *val = ktime_to_ms(kt);
699 static int pvt_write_timeout(struct pvt_hwmon *pvt, long val)
706 rate = clk_get_rate(pvt->clks[PVT_CLOCK_REF].clk);
711 * If alarms are enabled, the requested timeout must be divided
712 * between all available sensors to have the requested delay
713 * applicable to each individual sensor.
715 kt = ms_to_ktime(val);
716 #if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
717 kt = ktime_divns(kt, PVT_SENSORS_NUM);
721 * Subtract a constant lag, which always persists due to the limited
722 * PVT sampling rate. Make sure the timeout is not negative.
724 kt = ktime_sub_ns(kt, PVT_TOUT_MIN);
725 if (ktime_to_ns(kt) < 0)
726 kt = ktime_set(0, 0);
729 * Finally recalculate the timeout in terms of the reference clock
732 data = ktime_divns(kt * rate, NSEC_PER_SEC);
735 * Update the measurements delay, but lock the interface first, since
736 * we have to disable PVT in order to have the new delay actually
739 ret = mutex_lock_interruptible(&pvt->iface_mtx);
743 pvt_set_tout(pvt, data);
745 mutex_unlock(&pvt->iface_mtx);
750 static int pvt_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
751 u32 attr, int ch, long *val)
753 struct pvt_hwmon *pvt = dev_get_drvdata(dev);
755 if (!pvt_hwmon_channel_is_valid(type, ch))
761 case hwmon_chip_update_interval:
762 return pvt_read_timeout(pvt, val);
767 case hwmon_temp_input:
768 return pvt_read_data(pvt, ch, val);
769 case hwmon_temp_type:
773 return pvt_read_limit(pvt, ch, true, val);
775 return pvt_read_limit(pvt, ch, false, val);
776 case hwmon_temp_min_alarm:
777 return pvt_read_alarm(pvt, ch, true, val);
778 case hwmon_temp_max_alarm:
779 return pvt_read_alarm(pvt, ch, false, val);
780 case hwmon_temp_offset:
781 return pvt_read_trim(pvt, val);
787 return pvt_read_data(pvt, PVT_VOLT + ch, val);
789 return pvt_read_limit(pvt, PVT_VOLT + ch, true, val);
791 return pvt_read_limit(pvt, PVT_VOLT + ch, false, val);
792 case hwmon_in_min_alarm:
793 return pvt_read_alarm(pvt, PVT_VOLT + ch, true, val);
794 case hwmon_in_max_alarm:
795 return pvt_read_alarm(pvt, PVT_VOLT + ch, false, val);
805 static int pvt_hwmon_read_string(struct device *dev,
806 enum hwmon_sensor_types type,
807 u32 attr, int ch, const char **str)
809 if (!pvt_hwmon_channel_is_valid(type, ch))
815 case hwmon_temp_label:
816 *str = pvt_info[ch].label;
823 *str = pvt_info[PVT_VOLT + ch].label;
834 static int pvt_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
835 u32 attr, int ch, long val)
837 struct pvt_hwmon *pvt = dev_get_drvdata(dev);
839 if (!pvt_hwmon_channel_is_valid(type, ch))
845 case hwmon_chip_update_interval:
846 return pvt_write_timeout(pvt, val);
852 return pvt_write_limit(pvt, ch, true, val);
854 return pvt_write_limit(pvt, ch, false, val);
855 case hwmon_temp_offset:
856 return pvt_write_trim(pvt, val);
862 return pvt_write_limit(pvt, PVT_VOLT + ch, true, val);
864 return pvt_write_limit(pvt, PVT_VOLT + ch, false, val);
874 static const struct hwmon_ops pvt_hwmon_ops = {
875 .is_visible = pvt_hwmon_is_visible,
876 .read = pvt_hwmon_read,
877 .read_string = pvt_hwmon_read_string,
878 .write = pvt_hwmon_write
881 static const struct hwmon_chip_info pvt_hwmon_info = {
882 .ops = &pvt_hwmon_ops,
883 .info = pvt_channel_info
886 static void pvt_clear_data(void *data)
888 struct pvt_hwmon *pvt = data;
889 #if !defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
892 for (idx = 0; idx < PVT_SENSORS_NUM; ++idx)
893 complete_all(&pvt->cache[idx].conversion);
896 mutex_destroy(&pvt->iface_mtx);
899 static struct pvt_hwmon *pvt_create_data(struct platform_device *pdev)
901 struct device *dev = &pdev->dev;
902 struct pvt_hwmon *pvt;
905 pvt = devm_kzalloc(dev, sizeof(*pvt), GFP_KERNEL);
907 return ERR_PTR(-ENOMEM);
909 ret = devm_add_action(dev, pvt_clear_data, pvt);
911 dev_err(dev, "Can't add PVT data clear action\n");
916 pvt->sensor = PVT_SENSOR_FIRST;
917 mutex_init(&pvt->iface_mtx);
919 #if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
920 for (idx = 0; idx < PVT_SENSORS_NUM; ++idx)
921 seqlock_init(&pvt->cache[idx].data_seqlock);
923 for (idx = 0; idx < PVT_SENSORS_NUM; ++idx)
924 init_completion(&pvt->cache[idx].conversion);
930 static int pvt_request_regs(struct pvt_hwmon *pvt)
932 struct platform_device *pdev = to_platform_device(pvt->dev);
933 struct resource *res;
935 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
937 dev_err(pvt->dev, "Couldn't find PVT memresource\n");
941 pvt->regs = devm_ioremap_resource(pvt->dev, res);
942 if (IS_ERR(pvt->regs)) {
943 dev_err(pvt->dev, "Couldn't map PVT registers\n");
944 return PTR_ERR(pvt->regs);
950 static void pvt_disable_clks(void *data)
952 struct pvt_hwmon *pvt = data;
954 clk_bulk_disable_unprepare(PVT_CLOCK_NUM, pvt->clks);
957 static int pvt_request_clks(struct pvt_hwmon *pvt)
961 pvt->clks[PVT_CLOCK_APB].id = "pclk";
962 pvt->clks[PVT_CLOCK_REF].id = "ref";
964 ret = devm_clk_bulk_get(pvt->dev, PVT_CLOCK_NUM, pvt->clks);
966 dev_err(pvt->dev, "Couldn't get PVT clocks descriptors\n");
970 ret = clk_bulk_prepare_enable(PVT_CLOCK_NUM, pvt->clks);
972 dev_err(pvt->dev, "Couldn't enable the PVT clocks\n");
976 ret = devm_add_action_or_reset(pvt->dev, pvt_disable_clks, pvt);
978 dev_err(pvt->dev, "Can't add PVT clocks disable action\n");
985 static void pvt_init_iface(struct pvt_hwmon *pvt)
990 * Make sure all interrupts and controller are disabled so not to
991 * accidentally have ISR executed before the driver data is fully
992 * initialized. Clear the IRQ status as well.
994 pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_ALL, PVT_INTR_ALL);
995 pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
996 readl(pvt->regs + PVT_CLR_INTR);
997 readl(pvt->regs + PVT_DATA);
999 /* Setup default sensor mode, timeout and temperature trim. */
1000 pvt_set_mode(pvt, pvt_info[pvt->sensor].mode);
1001 pvt_set_tout(pvt, PVT_TOUT_DEF);
1003 trim = PVT_TRIM_DEF;
1004 if (!of_property_read_u32(pvt->dev->of_node,
1005 "baikal,pvt-temp-offset-millicelsius", &temp))
1006 trim = pvt_calc_trim(temp);
1008 pvt_set_trim(pvt, trim);
1011 static int pvt_request_irq(struct pvt_hwmon *pvt)
1013 struct platform_device *pdev = to_platform_device(pvt->dev);
1016 pvt->irq = platform_get_irq(pdev, 0);
1020 ret = devm_request_threaded_irq(pvt->dev, pvt->irq,
1021 pvt_hard_isr, pvt_soft_isr,
1022 #if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
1023 IRQF_SHARED | IRQF_TRIGGER_HIGH |
1026 IRQF_SHARED | IRQF_TRIGGER_HIGH,
1030 dev_err(pvt->dev, "Couldn't request PVT IRQ\n");
1037 static int pvt_create_hwmon(struct pvt_hwmon *pvt)
1039 pvt->hwmon = devm_hwmon_device_register_with_info(pvt->dev, "pvt", pvt,
1040 &pvt_hwmon_info, NULL);
1041 if (IS_ERR(pvt->hwmon)) {
1042 dev_err(pvt->dev, "Couldn't create hwmon device\n");
1043 return PTR_ERR(pvt->hwmon);
1049 #if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
1051 static void pvt_disable_iface(void *data)
1053 struct pvt_hwmon *pvt = data;
1055 mutex_lock(&pvt->iface_mtx);
1056 pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
1057 pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID,
1059 mutex_unlock(&pvt->iface_mtx);
1062 static int pvt_enable_iface(struct pvt_hwmon *pvt)
1066 ret = devm_add_action(pvt->dev, pvt_disable_iface, pvt);
1068 dev_err(pvt->dev, "Can't add PVT disable interface action\n");
1073 * Enable sensors data conversion and IRQ. We need to lock the
1074 * interface mutex since hwmon has just been created and the
1075 * corresponding sysfs files are accessible from user-space,
1076 * which theoretically may cause races.
1078 mutex_lock(&pvt->iface_mtx);
1079 pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID, 0);
1080 pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, PVT_CTRL_EN);
1081 mutex_unlock(&pvt->iface_mtx);
1086 #else /* !CONFIG_SENSORS_BT1_PVT_ALARMS */
1088 static int pvt_enable_iface(struct pvt_hwmon *pvt)
1093 #endif /* !CONFIG_SENSORS_BT1_PVT_ALARMS */
1095 static int pvt_probe(struct platform_device *pdev)
1097 struct pvt_hwmon *pvt;
1100 pvt = pvt_create_data(pdev);
1102 return PTR_ERR(pvt);
1104 ret = pvt_request_regs(pvt);
1108 ret = pvt_request_clks(pvt);
1112 pvt_init_iface(pvt);
1114 ret = pvt_request_irq(pvt);
1118 ret = pvt_create_hwmon(pvt);
1122 ret = pvt_enable_iface(pvt);
1129 static const struct of_device_id pvt_of_match[] = {
1130 { .compatible = "baikal,bt1-pvt" },
1133 MODULE_DEVICE_TABLE(of, pvt_of_match);
1135 static struct platform_driver pvt_driver = {
1139 .of_match_table = pvt_of_match
1142 module_platform_driver(pvt_driver);
1145 MODULE_DESCRIPTION("Baikal-T1 PVT driver");
1146 MODULE_LICENSE("GPL v2");