Linux Audio

Check our new training course

Loading...
v6.8
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * Copyright (C) 2020 BAIKAL ELECTRONICS, JSC
   4 *
   5 * Authors:
   6 *   Maxim Kaurkin <maxim.kaurkin@baikalelectronics.ru>
   7 *   Serge Semin <Sergey.Semin@baikalelectronics.ru>
   8 *
   9 * Baikal-T1 Process, Voltage, Temperature sensor driver
  10 */
  11
  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>
  21#include <linux/io.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>
  27#include <linux/of.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>
  33
  34#include "bt1-pvt.h"
  35
  36/*
  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.
  40 */
  41static 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),
  47};
  48
  49/*
  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) +
  53 *     1.7204e2,
  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
  62 * the alterations:
  63 * N = (18322e-20*(T^4) + 2343e-13*(T^3) + 87018e-9*(T^2) + 39269e-3*T +
  64 *     17204e2) / 1e4,
  65 * T = -16743e-12*(D^4) + 81542e-9*(D^3) - 182010e-6*(D^2) + 310200e-3*D -
  66 *     48380,
  67 * where T = [-48380, 147438] mC and N = [0, 1023].
  68 */
  69static const struct polynomial __maybe_unused poly_temp_to_N = {
  70	.total_divider = 10000,
  71	.terms = {
  72		{4, 18322, 10000, 10000},
  73		{3, 2343, 10000, 10},
  74		{2, 87018, 10000, 10},
  75		{1, 39269, 1000, 1},
  76		{0, 1720400, 1, 1}
  77	}
  78};
  79
  80static const struct polynomial poly_N_to_temp = {
  81	.total_divider = 1,
  82	.terms = {
  83		{4, -16743, 1000, 1},
  84		{3, 81542, 1000, 1},
  85		{2, -182010, 1000, 1},
  86		{1, 310200, 1000, 1},
  87		{0, -48380, 1, 1}
  88	}
  89};
  90
  91/*
  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.
 100 */
 101static const struct polynomial __maybe_unused poly_volt_to_N = {
 102	.total_divider = 10,
 103	.terms = {
 104		{1, 18658, 1000, 1},
 105		{0, -11572, 1, 1}
 106	}
 107};
 108
 109static const struct polynomial poly_N_to_volt = {
 110	.total_divider = 10,
 111	.terms = {
 112		{1, 100000, 18658, 1},
 113		{0, 115720000, 1, 18658}
 114	}
 115};
 116
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 117static inline u32 pvt_update(void __iomem *reg, u32 mask, u32 data)
 118{
 119	u32 old;
 120
 121	old = readl_relaxed(reg);
 122	writel((old & ~mask) | (data & mask), reg);
 123
 124	return old & mask;
 125}
 126
 127/*
 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.
 133 */
 134static inline void pvt_set_mode(struct pvt_hwmon *pvt, u32 mode)
 135{
 136	u32 old;
 137
 138	mode = FIELD_PREP(PVT_CTRL_MODE_MASK, mode);
 139
 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,
 142		   mode | old);
 143}
 144
 145static inline u32 pvt_calc_trim(long temp)
 146{
 147	temp = clamp_val(temp, 0, PVT_TRIM_TEMP);
 148
 149	return DIV_ROUND_UP(temp, PVT_TRIM_STEP);
 150}
 151
 152static inline void pvt_set_trim(struct pvt_hwmon *pvt, u32 trim)
 153{
 154	u32 old;
 155
 156	trim = FIELD_PREP(PVT_CTRL_TRIM_MASK, trim);
 157
 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,
 160		   trim | old);
 161}
 162
 163static inline void pvt_set_tout(struct pvt_hwmon *pvt, u32 tout)
 164{
 165	u32 old;
 166
 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);
 170}
 171
 172/*
 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
 179 * date.
 180 *
 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.
 198 */
 199
 200#if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
 201
 202#define pvt_hard_isr NULL
 203
 204static irqreturn_t pvt_soft_isr(int irq, void *data)
 205{
 206	const struct pvt_sensor_info *info;
 207	struct pvt_hwmon *pvt = data;
 208	struct pvt_cache *cache;
 209	u32 val, thres_sts, old;
 210
 211	/*
 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.
 215	 */
 216	thres_sts = readl(pvt->regs + PVT_RAW_INTR_STAT);
 217
 218	/*
 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.
 222	 */
 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);
 227
 228	/*
 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
 235	 * interrupts back.
 236	 */
 237	mutex_lock(&pvt->iface_mtx);
 238
 239	old = pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID,
 240			 PVT_INTR_DVALID);
 241
 242	val = readl(pvt->regs + PVT_DATA);
 243
 244	pvt_set_mode(pvt, pvt_info[pvt->sensor].mode);
 245
 246	pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID, old);
 247
 248	mutex_unlock(&pvt->iface_mtx);
 249
 250	/*
 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
 253	 * data.
 254	 */
 255	write_seqlock(&cache->data_seqlock);
 256
 257	cache->data = FIELD_GET(PVT_DATA_DATA_MASK, val);
 258
 259	write_sequnlock(&cache->data_seqlock);
 260
 261	/*
 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.
 266	 */
 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,
 270				   info->channel);
 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,
 274				   info->channel);
 275	}
 276
 277	return IRQ_HANDLED;
 278}
 279
 280static inline umode_t pvt_limit_is_visible(enum pvt_sensor_type type)
 281{
 282	return 0644;
 283}
 284
 285static inline umode_t pvt_alarm_is_visible(enum pvt_sensor_type type)
 286{
 287	return 0444;
 288}
 289
 290static int pvt_read_data(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
 291			 long *val)
 292{
 293	struct pvt_cache *cache = &pvt->cache[type];
 294	unsigned int seq;
 295	u32 data;
 296
 297	do {
 298		seq = read_seqbegin(&cache->data_seqlock);
 299		data = cache->data;
 300	} while (read_seqretry(&cache->data_seqlock, seq));
 301
 302	if (type == PVT_TEMP)
 303		*val = polynomial_calc(&poly_N_to_temp, data);
 304	else
 305		*val = polynomial_calc(&poly_N_to_volt, data);
 306
 307	return 0;
 308}
 309
 310static int pvt_read_limit(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
 311			  bool is_low, long *val)
 312{
 313	u32 data;
 314
 315	/* No need in serialization, since it is just read from MMIO. */
 316	data = readl(pvt->regs + pvt_info[type].thres_base);
 317
 318	if (is_low)
 319		data = FIELD_GET(PVT_THRES_LO_MASK, data);
 320	else
 321		data = FIELD_GET(PVT_THRES_HI_MASK, data);
 322
 323	if (type == PVT_TEMP)
 324		*val = polynomial_calc(&poly_N_to_temp, data);
 325	else
 326		*val = polynomial_calc(&poly_N_to_volt, data);
 327
 328	return 0;
 329}
 330
 331static int pvt_write_limit(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
 332			   bool is_low, long val)
 333{
 334	u32 data, limit, mask;
 335	int ret;
 336
 337	if (type == PVT_TEMP) {
 338		val = clamp(val, PVT_TEMP_MIN, PVT_TEMP_MAX);
 339		data = polynomial_calc(&poly_temp_to_N, val);
 340	} else {
 341		val = clamp(val, PVT_VOLT_MIN, PVT_VOLT_MAX);
 342		data = polynomial_calc(&poly_volt_to_N, val);
 343	}
 344
 345	/* Serialize limit update, since a part of the register is changed. */
 346	ret = mutex_lock_interruptible(&pvt->iface_mtx);
 347	if (ret)
 348		return ret;
 349
 350	/* Make sure the upper and lower ranges don't intersect. */
 351	limit = readl(pvt->regs + pvt_info[type].thres_base);
 352	if (is_low) {
 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;
 357	} else {
 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;
 362	}
 363
 364	pvt_update(pvt->regs + pvt_info[type].thres_base, mask, data);
 365
 366	mutex_unlock(&pvt->iface_mtx);
 367
 368	return 0;
 369}
 370
 371static int pvt_read_alarm(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
 372			  bool is_low, long *val)
 373{
 374	if (is_low)
 375		*val = !!READ_ONCE(pvt->cache[type].thres_sts_lo);
 376	else
 377		*val = !!READ_ONCE(pvt->cache[type].thres_sts_hi);
 378
 379	return 0;
 380}
 381
 382static 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 |
 389			   HWMON_T_OFFSET),
 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),
 403	NULL
 404};
 405
 406#else /* !CONFIG_SENSORS_BT1_PVT_ALARMS */
 407
 408static irqreturn_t pvt_hard_isr(int irq, void *data)
 409{
 410	struct pvt_hwmon *pvt = data;
 411	struct pvt_cache *cache;
 412	u32 val;
 413
 414	/*
 415	 * Mask the DVALID interrupt so after exiting from the handler a
 416	 * repeated conversion wouldn't happen.
 417	 */
 418	pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID,
 419		   PVT_INTR_DVALID);
 420
 421	/*
 422	 * Nothing special for alarm-less driver. Just read the data, update
 423	 * the cache and notify a waiter of this event.
 424	 */
 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");
 428		return IRQ_HANDLED;
 429	}
 430
 431	cache = &pvt->cache[pvt->sensor];
 432
 433	WRITE_ONCE(cache->data, FIELD_GET(PVT_DATA_DATA_MASK, val));
 434
 435	complete(&cache->conversion);
 436
 437	return IRQ_HANDLED;
 438}
 439
 440#define pvt_soft_isr NULL
 441
 442static inline umode_t pvt_limit_is_visible(enum pvt_sensor_type type)
 443{
 444	return 0;
 445}
 446
 447static inline umode_t pvt_alarm_is_visible(enum pvt_sensor_type type)
 448{
 449	return 0;
 450}
 451
 452static int pvt_read_data(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
 453			 long *val)
 454{
 455	struct pvt_cache *cache = &pvt->cache[type];
 456	unsigned long timeout;
 457	u32 data;
 458	int ret;
 459
 460	/*
 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.
 465	 */
 466	ret = mutex_lock_interruptible(&pvt->iface_mtx);
 467	if (ret)
 468		return ret;
 469
 470	pvt->sensor = type;
 471	pvt_set_mode(pvt, pvt_info[type].mode);
 472
 473	/*
 474	 * Unmask the DVALID interrupt and enable the sensors conversions.
 475	 * Do the reverse procedure when conversion is done.
 476	 */
 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);
 479
 480	/*
 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.
 485	 */
 486	timeout = 2 * usecs_to_jiffies(ktime_to_us(pvt->timeout));
 487	ret = wait_for_completion_timeout(&cache->conversion, timeout);
 488
 489	pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
 490	pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID,
 491		   PVT_INTR_DVALID);
 492
 493	data = READ_ONCE(cache->data);
 494
 495	mutex_unlock(&pvt->iface_mtx);
 496
 497	if (!ret)
 498		return -ETIMEDOUT;
 499
 500	if (type == PVT_TEMP)
 501		*val = polynomial_calc(&poly_N_to_temp, data);
 502	else
 503		*val = polynomial_calc(&poly_N_to_volt, data);
 504
 505	return 0;
 506}
 507
 508static int pvt_read_limit(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
 509			  bool is_low, long *val)
 510{
 511	return -EOPNOTSUPP;
 512}
 513
 514static int pvt_write_limit(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
 515			   bool is_low, long val)
 516{
 517	return -EOPNOTSUPP;
 518}
 519
 520static int pvt_read_alarm(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
 521			  bool is_low, long *val)
 522{
 523	return -EOPNOTSUPP;
 524}
 525
 526static 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 |
 531			   HWMON_T_OFFSET),
 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),
 537	NULL
 538};
 539
 540#endif /* !CONFIG_SENSORS_BT1_PVT_ALARMS */
 541
 542static inline bool pvt_hwmon_channel_is_valid(enum hwmon_sensor_types type,
 543					      int ch)
 544{
 545	switch (type) {
 546	case hwmon_temp:
 547		if (ch < 0 || ch >= PVT_TEMP_CHS)
 548			return false;
 549		break;
 550	case hwmon_in:
 551		if (ch < 0 || ch >= PVT_VOLT_CHS)
 552			return false;
 553		break;
 554	default:
 555		break;
 556	}
 557
 558	/* The rest of the types are independent from the channel number. */
 559	return true;
 560}
 561
 562static umode_t pvt_hwmon_is_visible(const void *data,
 563				    enum hwmon_sensor_types type,
 564				    u32 attr, int ch)
 565{
 566	if (!pvt_hwmon_channel_is_valid(type, ch))
 567		return 0;
 568
 569	switch (type) {
 570	case hwmon_chip:
 571		switch (attr) {
 572		case hwmon_chip_update_interval:
 573			return 0644;
 574		}
 575		break;
 576	case hwmon_temp:
 577		switch (attr) {
 578		case hwmon_temp_input:
 579		case hwmon_temp_type:
 580		case hwmon_temp_label:
 581			return 0444;
 582		case hwmon_temp_min:
 583		case hwmon_temp_max:
 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:
 589			return 0644;
 590		}
 591		break;
 592	case hwmon_in:
 593		switch (attr) {
 594		case hwmon_in_input:
 595		case hwmon_in_label:
 596			return 0444;
 597		case hwmon_in_min:
 598		case hwmon_in_max:
 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);
 603		}
 604		break;
 605	default:
 606		break;
 607	}
 608
 609	return 0;
 610}
 611
 612static int pvt_read_trim(struct pvt_hwmon *pvt, long *val)
 613{
 614	u32 data;
 615
 616	data = readl(pvt->regs + PVT_CTRL);
 617	*val = FIELD_GET(PVT_CTRL_TRIM_MASK, data) * PVT_TRIM_STEP;
 618
 619	return 0;
 620}
 621
 622static int pvt_write_trim(struct pvt_hwmon *pvt, long val)
 623{
 624	u32 trim;
 625	int ret;
 626
 627	/*
 628	 * Serialize trim update, since a part of the register is changed and
 629	 * the controller is supposed to be disabled during this operation.
 630	 */
 631	ret = mutex_lock_interruptible(&pvt->iface_mtx);
 632	if (ret)
 633		return ret;
 634
 635	trim = pvt_calc_trim(val);
 636	pvt_set_trim(pvt, trim);
 637
 638	mutex_unlock(&pvt->iface_mtx);
 639
 640	return 0;
 641}
 642
 643static int pvt_read_timeout(struct pvt_hwmon *pvt, long *val)
 644{
 645	int ret;
 
 
 646
 647	ret = mutex_lock_interruptible(&pvt->iface_mtx);
 648	if (ret)
 649		return ret;
 650
 651	/* Return the result in msec as hwmon sysfs interface requires. */
 652	*val = ktime_to_ms(pvt->timeout);
 
 
 
 
 653
 654	mutex_unlock(&pvt->iface_mtx);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 655
 656	return 0;
 657}
 658
 659static int pvt_write_timeout(struct pvt_hwmon *pvt, long val)
 660{
 661	unsigned long rate;
 662	ktime_t kt, cache;
 663	u32 data;
 664	int ret;
 665
 666	rate = clk_get_rate(pvt->clks[PVT_CLOCK_REF].clk);
 667	if (!rate)
 668		return -ENODEV;
 669
 670	/*
 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.
 674	 */
 675	cache = kt = ms_to_ktime(val);
 676#if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
 677	kt = ktime_divns(kt, PVT_SENSORS_NUM);
 678#endif
 679
 680	/*
 681	 * Subtract a constant lag, which always persists due to the limited
 682	 * PVT sampling rate. Make sure the timeout is not negative.
 683	 */
 684	kt = ktime_sub_ns(kt, PVT_TOUT_MIN);
 685	if (ktime_to_ns(kt) < 0)
 686		kt = ktime_set(0, 0);
 687
 688	/*
 689	 * Finally recalculate the timeout in terms of the reference clock
 690	 * period.
 691	 */
 692	data = ktime_divns(kt * rate, NSEC_PER_SEC);
 693
 694	/*
 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
 697	 * updated.
 698	 */
 699	ret = mutex_lock_interruptible(&pvt->iface_mtx);
 700	if (ret)
 701		return ret;
 702
 703	pvt_set_tout(pvt, data);
 704	pvt->timeout = cache;
 705
 706	mutex_unlock(&pvt->iface_mtx);
 707
 708	return 0;
 709}
 710
 711static int pvt_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
 712			  u32 attr, int ch, long *val)
 713{
 714	struct pvt_hwmon *pvt = dev_get_drvdata(dev);
 715
 716	if (!pvt_hwmon_channel_is_valid(type, ch))
 717		return -EINVAL;
 718
 719	switch (type) {
 720	case hwmon_chip:
 721		switch (attr) {
 722		case hwmon_chip_update_interval:
 723			return pvt_read_timeout(pvt, val);
 724		}
 725		break;
 726	case hwmon_temp:
 727		switch (attr) {
 728		case hwmon_temp_input:
 729			return pvt_read_data(pvt, ch, val);
 730		case hwmon_temp_type:
 731			*val = 1;
 732			return 0;
 733		case hwmon_temp_min:
 734			return pvt_read_limit(pvt, ch, true, val);
 735		case hwmon_temp_max:
 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);
 743		}
 744		break;
 745	case hwmon_in:
 746		switch (attr) {
 747		case hwmon_in_input:
 748			return pvt_read_data(pvt, PVT_VOLT + ch, val);
 749		case hwmon_in_min:
 750			return pvt_read_limit(pvt, PVT_VOLT + ch, true, val);
 751		case hwmon_in_max:
 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);
 757		}
 758		break;
 759	default:
 760		break;
 761	}
 762
 763	return -EOPNOTSUPP;
 764}
 765
 766static int pvt_hwmon_read_string(struct device *dev,
 767				 enum hwmon_sensor_types type,
 768				 u32 attr, int ch, const char **str)
 769{
 770	if (!pvt_hwmon_channel_is_valid(type, ch))
 771		return -EINVAL;
 772
 773	switch (type) {
 774	case hwmon_temp:
 775		switch (attr) {
 776		case hwmon_temp_label:
 777			*str = pvt_info[ch].label;
 778			return 0;
 779		}
 780		break;
 781	case hwmon_in:
 782		switch (attr) {
 783		case hwmon_in_label:
 784			*str = pvt_info[PVT_VOLT + ch].label;
 785			return 0;
 786		}
 787		break;
 788	default:
 789		break;
 790	}
 791
 792	return -EOPNOTSUPP;
 793}
 794
 795static int pvt_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
 796			   u32 attr, int ch, long val)
 797{
 798	struct pvt_hwmon *pvt = dev_get_drvdata(dev);
 799
 800	if (!pvt_hwmon_channel_is_valid(type, ch))
 801		return -EINVAL;
 802
 803	switch (type) {
 804	case hwmon_chip:
 805		switch (attr) {
 806		case hwmon_chip_update_interval:
 807			return pvt_write_timeout(pvt, val);
 808		}
 809		break;
 810	case hwmon_temp:
 811		switch (attr) {
 812		case hwmon_temp_min:
 813			return pvt_write_limit(pvt, ch, true, val);
 814		case hwmon_temp_max:
 815			return pvt_write_limit(pvt, ch, false, val);
 816		case hwmon_temp_offset:
 817			return pvt_write_trim(pvt, val);
 818		}
 819		break;
 820	case hwmon_in:
 821		switch (attr) {
 822		case hwmon_in_min:
 823			return pvt_write_limit(pvt, PVT_VOLT + ch, true, val);
 824		case hwmon_in_max:
 825			return pvt_write_limit(pvt, PVT_VOLT + ch, false, val);
 826		}
 827		break;
 828	default:
 829		break;
 830	}
 831
 832	return -EOPNOTSUPP;
 833}
 834
 835static 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
 840};
 841
 842static const struct hwmon_chip_info pvt_hwmon_info = {
 843	.ops = &pvt_hwmon_ops,
 844	.info = pvt_channel_info
 845};
 846
 847static void pvt_clear_data(void *data)
 848{
 849	struct pvt_hwmon *pvt = data;
 850#if !defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
 851	int idx;
 852
 853	for (idx = 0; idx < PVT_SENSORS_NUM; ++idx)
 854		complete_all(&pvt->cache[idx].conversion);
 855#endif
 856
 857	mutex_destroy(&pvt->iface_mtx);
 858}
 859
 860static struct pvt_hwmon *pvt_create_data(struct platform_device *pdev)
 861{
 862	struct device *dev = &pdev->dev;
 863	struct pvt_hwmon *pvt;
 864	int ret, idx;
 865
 866	pvt = devm_kzalloc(dev, sizeof(*pvt), GFP_KERNEL);
 867	if (!pvt)
 868		return ERR_PTR(-ENOMEM);
 869
 870	ret = devm_add_action(dev, pvt_clear_data, pvt);
 871	if (ret) {
 872		dev_err(dev, "Can't add PVT data clear action\n");
 873		return ERR_PTR(ret);
 874	}
 875
 876	pvt->dev = dev;
 877	pvt->sensor = PVT_SENSOR_FIRST;
 878	mutex_init(&pvt->iface_mtx);
 879
 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);
 883#else
 884	for (idx = 0; idx < PVT_SENSORS_NUM; ++idx)
 885		init_completion(&pvt->cache[idx].conversion);
 886#endif
 887
 888	return pvt;
 889}
 890
 891static int pvt_request_regs(struct pvt_hwmon *pvt)
 892{
 893	struct platform_device *pdev = to_platform_device(pvt->dev);
 
 894
 895	pvt->regs = devm_platform_ioremap_resource(pdev, 0);
 896	if (IS_ERR(pvt->regs))
 
 
 
 
 
 
 
 897		return PTR_ERR(pvt->regs);
 
 898
 899	return 0;
 900}
 901
 902static void pvt_disable_clks(void *data)
 903{
 904	struct pvt_hwmon *pvt = data;
 905
 906	clk_bulk_disable_unprepare(PVT_CLOCK_NUM, pvt->clks);
 907}
 908
 909static int pvt_request_clks(struct pvt_hwmon *pvt)
 910{
 911	int ret;
 912
 913	pvt->clks[PVT_CLOCK_APB].id = "pclk";
 914	pvt->clks[PVT_CLOCK_REF].id = "ref";
 915
 916	ret = devm_clk_bulk_get(pvt->dev, PVT_CLOCK_NUM, pvt->clks);
 917	if (ret) {
 918		dev_err(pvt->dev, "Couldn't get PVT clocks descriptors\n");
 919		return ret;
 920	}
 921
 922	ret = clk_bulk_prepare_enable(PVT_CLOCK_NUM, pvt->clks);
 923	if (ret) {
 924		dev_err(pvt->dev, "Couldn't enable the PVT clocks\n");
 925		return ret;
 926	}
 927
 928	ret = devm_add_action_or_reset(pvt->dev, pvt_disable_clks, pvt);
 929	if (ret) {
 930		dev_err(pvt->dev, "Can't add PVT clocks disable action\n");
 931		return ret;
 932	}
 933
 934	return 0;
 935}
 936
 937static int pvt_check_pwr(struct pvt_hwmon *pvt)
 938{
 939	unsigned long tout;
 940	int ret = 0;
 941	u32 data;
 942
 943	/*
 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
 951	 * problem.
 952	 */
 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);
 957
 958	tout = PVT_TOUT_MIN / NSEC_PER_USEC;
 959	usleep_range(tout, 2 * tout);
 960
 961	data = readl(pvt->regs + PVT_DATA);
 962	if (!(data & PVT_DATA_VALID)) {
 963		ret = -ENODEV;
 964		dev_err(pvt->dev, "Sensor is powered down\n");
 965	}
 966
 967	pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
 968
 969	return ret;
 970}
 971
 972static int pvt_init_iface(struct pvt_hwmon *pvt)
 973{
 974	unsigned long rate;
 975	u32 trim, temp;
 976
 977	rate = clk_get_rate(pvt->clks[PVT_CLOCK_REF].clk);
 978	if (!rate) {
 979		dev_err(pvt->dev, "Invalid reference clock rate\n");
 980		return -ENODEV;
 981	}
 982
 983	/*
 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.
 987	 */
 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);
 992
 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);
 996
 997	/*
 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)
1009	 */
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);
1014#else
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);
1018#endif
1019
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);
1024
1025	pvt_set_trim(pvt, trim);
1026
1027	return 0;
1028}
1029
1030static int pvt_request_irq(struct pvt_hwmon *pvt)
1031{
1032	struct platform_device *pdev = to_platform_device(pvt->dev);
1033	int ret;
1034
1035	pvt->irq = platform_get_irq(pdev, 0);
1036	if (pvt->irq < 0)
1037		return pvt->irq;
1038
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 |
1043					IRQF_ONESHOT,
1044#else
1045					IRQF_SHARED | IRQF_TRIGGER_HIGH,
1046#endif
1047					"pvt", pvt);
1048	if (ret) {
1049		dev_err(pvt->dev, "Couldn't request PVT IRQ\n");
1050		return ret;
1051	}
1052
1053	return 0;
1054}
1055
1056static int pvt_create_hwmon(struct pvt_hwmon *pvt)
1057{
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);
1063	}
1064
1065	return 0;
1066}
1067
1068#if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
1069
1070static void pvt_disable_iface(void *data)
1071{
1072	struct pvt_hwmon *pvt = data;
1073
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,
1077		   PVT_INTR_DVALID);
1078	mutex_unlock(&pvt->iface_mtx);
1079}
1080
1081static int pvt_enable_iface(struct pvt_hwmon *pvt)
1082{
1083	int ret;
1084
1085	ret = devm_add_action(pvt->dev, pvt_disable_iface, pvt);
1086	if (ret) {
1087		dev_err(pvt->dev, "Can't add PVT disable interface action\n");
1088		return ret;
1089	}
1090
1091	/*
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.
1096	 */
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);
1101
1102	return 0;
1103}
1104
1105#else /* !CONFIG_SENSORS_BT1_PVT_ALARMS */
1106
1107static int pvt_enable_iface(struct pvt_hwmon *pvt)
1108{
1109	return 0;
1110}
1111
1112#endif /* !CONFIG_SENSORS_BT1_PVT_ALARMS */
1113
1114static int pvt_probe(struct platform_device *pdev)
1115{
1116	struct pvt_hwmon *pvt;
1117	int ret;
1118
1119	pvt = pvt_create_data(pdev);
1120	if (IS_ERR(pvt))
1121		return PTR_ERR(pvt);
1122
1123	ret = pvt_request_regs(pvt);
1124	if (ret)
1125		return ret;
1126
1127	ret = pvt_request_clks(pvt);
1128	if (ret)
1129		return ret;
1130
1131	ret = pvt_check_pwr(pvt);
1132	if (ret)
1133		return ret;
1134
1135	ret = pvt_init_iface(pvt);
1136	if (ret)
1137		return ret;
1138
1139	ret = pvt_request_irq(pvt);
1140	if (ret)
1141		return ret;
1142
1143	ret = pvt_create_hwmon(pvt);
1144	if (ret)
1145		return ret;
1146
1147	ret = pvt_enable_iface(pvt);
1148	if (ret)
1149		return ret;
1150
1151	return 0;
1152}
1153
1154static const struct of_device_id pvt_of_match[] = {
1155	{ .compatible = "baikal,bt1-pvt" },
1156	{ }
1157};
1158MODULE_DEVICE_TABLE(of, pvt_of_match);
1159
1160static struct platform_driver pvt_driver = {
1161	.probe = pvt_probe,
1162	.driver = {
1163		.name = "bt1-pvt",
1164		.of_match_table = pvt_of_match
1165	}
1166};
1167module_platform_driver(pvt_driver);
1168
1169MODULE_AUTHOR("Maxim Kaurkin <maxim.kaurkin@baikalelectronics.ru>");
1170MODULE_DESCRIPTION("Baikal-T1 PVT driver");
1171MODULE_LICENSE("GPL v2");
v5.9
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * Copyright (C) 2020 BAIKAL ELECTRONICS, JSC
   4 *
   5 * Authors:
   6 *   Maxim Kaurkin <maxim.kaurkin@baikalelectronics.ru>
   7 *   Serge Semin <Sergey.Semin@baikalelectronics.ru>
   8 *
   9 * Baikal-T1 Process, Voltage, Temperature sensor driver
  10 */
  11
  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>
  20#include <linux/io.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>
  26#include <linux/of.h>
  27#include <linux/platform_device.h>
 
  28#include <linux/seqlock.h>
  29#include <linux/sysfs.h>
  30#include <linux/types.h>
  31
  32#include "bt1-pvt.h"
  33
  34/*
  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.
  38 */
  39static 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),
  45};
  46
  47/*
  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) +
  51 *     1.7204e2,
  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
  60 * the alterations:
  61 * N = (18322e-20*(T^4) + 2343e-13*(T^3) + 87018e-9*(T^2) + 39269e-3*T +
  62 *     17204e2) / 1e4,
  63 * T = -16743e-12*(D^4) + 81542e-9*(D^3) - 182010e-6*(D^2) + 310200e-3*D -
  64 *     48380,
  65 * where T = [-48380, 147438] mC and N = [0, 1023].
  66 */
  67static const struct pvt_poly __maybe_unused poly_temp_to_N = {
  68	.total_divider = 10000,
  69	.terms = {
  70		{4, 18322, 10000, 10000},
  71		{3, 2343, 10000, 10},
  72		{2, 87018, 10000, 10},
  73		{1, 39269, 1000, 1},
  74		{0, 1720400, 1, 1}
  75	}
  76};
  77
  78static const struct pvt_poly poly_N_to_temp = {
  79	.total_divider = 1,
  80	.terms = {
  81		{4, -16743, 1000, 1},
  82		{3, 81542, 1000, 1},
  83		{2, -182010, 1000, 1},
  84		{1, 310200, 1000, 1},
  85		{0, -48380, 1, 1}
  86	}
  87};
  88
  89/*
  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.
  98 */
  99static const struct pvt_poly __maybe_unused poly_volt_to_N = {
 100	.total_divider = 10,
 101	.terms = {
 102		{1, 18658, 1000, 1},
 103		{0, -11572, 1, 1}
 104	}
 105};
 106
 107static const struct pvt_poly poly_N_to_volt = {
 108	.total_divider = 10,
 109	.terms = {
 110		{1, 100000, 18658, 1},
 111		{0, 115720000, 1, 18658}
 112	}
 113};
 114
 115/*
 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.
 123 */
 124static long pvt_calc_poly(const struct pvt_poly *poly, long data)
 125{
 126	const struct pvt_poly_term *term = poly->terms;
 127	long tmp, ret = 0;
 128	int deg;
 129
 130	do {
 131		tmp = term->coef;
 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);
 136
 137	return ret / poly->total_divider;
 138}
 139
 140static inline u32 pvt_update(void __iomem *reg, u32 mask, u32 data)
 141{
 142	u32 old;
 143
 144	old = readl_relaxed(reg);
 145	writel((old & ~mask) | (data & mask), reg);
 146
 147	return old & mask;
 148}
 149
 150/*
 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.
 156 */
 157static inline void pvt_set_mode(struct pvt_hwmon *pvt, u32 mode)
 158{
 159	u32 old;
 160
 161	mode = FIELD_PREP(PVT_CTRL_MODE_MASK, mode);
 162
 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,
 165		   mode | old);
 166}
 167
 168static inline u32 pvt_calc_trim(long temp)
 169{
 170	temp = clamp_val(temp, 0, PVT_TRIM_TEMP);
 171
 172	return DIV_ROUND_UP(temp, PVT_TRIM_STEP);
 173}
 174
 175static inline void pvt_set_trim(struct pvt_hwmon *pvt, u32 trim)
 176{
 177	u32 old;
 178
 179	trim = FIELD_PREP(PVT_CTRL_TRIM_MASK, trim);
 180
 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,
 183		   trim | old);
 184}
 185
 186static inline void pvt_set_tout(struct pvt_hwmon *pvt, u32 tout)
 187{
 188	u32 old;
 189
 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);
 193}
 194
 195/*
 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
 202 * date.
 203 *
 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.
 221 */
 222
 223#if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
 224
 225#define pvt_hard_isr NULL
 226
 227static irqreturn_t pvt_soft_isr(int irq, void *data)
 228{
 229	const struct pvt_sensor_info *info;
 230	struct pvt_hwmon *pvt = data;
 231	struct pvt_cache *cache;
 232	u32 val, thres_sts, old;
 233
 234	/*
 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.
 238	 */
 239	thres_sts = readl(pvt->regs + PVT_RAW_INTR_STAT);
 240
 241	/*
 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.
 245	 */
 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);
 250
 251	/*
 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
 258	 * interrupts back.
 259	 */
 260	mutex_lock(&pvt->iface_mtx);
 261
 262	old = pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID,
 263			 PVT_INTR_DVALID);
 264
 265	val = readl(pvt->regs + PVT_DATA);
 266
 267	pvt_set_mode(pvt, pvt_info[pvt->sensor].mode);
 268
 269	pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID, old);
 270
 271	mutex_unlock(&pvt->iface_mtx);
 272
 273	/*
 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
 276	 * data.
 277	 */
 278	write_seqlock(&cache->data_seqlock);
 279
 280	cache->data = FIELD_GET(PVT_DATA_DATA_MASK, val);
 281
 282	write_sequnlock(&cache->data_seqlock);
 283
 284	/*
 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.
 289	 */
 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,
 293				   info->channel);
 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,
 297				   info->channel);
 298	}
 299
 300	return IRQ_HANDLED;
 301}
 302
 303static inline umode_t pvt_limit_is_visible(enum pvt_sensor_type type)
 304{
 305	return 0644;
 306}
 307
 308static inline umode_t pvt_alarm_is_visible(enum pvt_sensor_type type)
 309{
 310	return 0444;
 311}
 312
 313static int pvt_read_data(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
 314			 long *val)
 315{
 316	struct pvt_cache *cache = &pvt->cache[type];
 317	unsigned int seq;
 318	u32 data;
 319
 320	do {
 321		seq = read_seqbegin(&cache->data_seqlock);
 322		data = cache->data;
 323	} while (read_seqretry(&cache->data_seqlock, seq));
 324
 325	if (type == PVT_TEMP)
 326		*val = pvt_calc_poly(&poly_N_to_temp, data);
 327	else
 328		*val = pvt_calc_poly(&poly_N_to_volt, data);
 329
 330	return 0;
 331}
 332
 333static int pvt_read_limit(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
 334			  bool is_low, long *val)
 335{
 336	u32 data;
 337
 338	/* No need in serialization, since it is just read from MMIO. */
 339	data = readl(pvt->regs + pvt_info[type].thres_base);
 340
 341	if (is_low)
 342		data = FIELD_GET(PVT_THRES_LO_MASK, data);
 343	else
 344		data = FIELD_GET(PVT_THRES_HI_MASK, data);
 345
 346	if (type == PVT_TEMP)
 347		*val = pvt_calc_poly(&poly_N_to_temp, data);
 348	else
 349		*val = pvt_calc_poly(&poly_N_to_volt, data);
 350
 351	return 0;
 352}
 353
 354static int pvt_write_limit(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
 355			   bool is_low, long val)
 356{
 357	u32 data, limit, mask;
 358	int ret;
 359
 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);
 363	} else {
 364		val = clamp(val, PVT_VOLT_MIN, PVT_VOLT_MAX);
 365		data = pvt_calc_poly(&poly_volt_to_N, val);
 366	}
 367
 368	/* Serialize limit update, since a part of the register is changed. */
 369	ret = mutex_lock_interruptible(&pvt->iface_mtx);
 370	if (ret)
 371		return ret;
 372
 373	/* Make sure the upper and lower ranges don't intersect. */
 374	limit = readl(pvt->regs + pvt_info[type].thres_base);
 375	if (is_low) {
 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;
 380	} else {
 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;
 385	}
 386
 387	pvt_update(pvt->regs + pvt_info[type].thres_base, mask, data);
 388
 389	mutex_unlock(&pvt->iface_mtx);
 390
 391	return 0;
 392}
 393
 394static int pvt_read_alarm(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
 395			  bool is_low, long *val)
 396{
 397	if (is_low)
 398		*val = !!READ_ONCE(pvt->cache[type].thres_sts_lo);
 399	else
 400		*val = !!READ_ONCE(pvt->cache[type].thres_sts_hi);
 401
 402	return 0;
 403}
 404
 405static 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 |
 412			   HWMON_T_OFFSET),
 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),
 426	NULL
 427};
 428
 429#else /* !CONFIG_SENSORS_BT1_PVT_ALARMS */
 430
 431static irqreturn_t pvt_hard_isr(int irq, void *data)
 432{
 433	struct pvt_hwmon *pvt = data;
 434	struct pvt_cache *cache;
 435	u32 val;
 436
 437	/*
 438	 * Mask the DVALID interrupt so after exiting from the handler a
 439	 * repeated conversion wouldn't happen.
 440	 */
 441	pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID,
 442		   PVT_INTR_DVALID);
 443
 444	/*
 445	 * Nothing special for alarm-less driver. Just read the data, update
 446	 * the cache and notify a waiter of this event.
 447	 */
 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");
 451		return IRQ_HANDLED;
 452	}
 453
 454	cache = &pvt->cache[pvt->sensor];
 455
 456	WRITE_ONCE(cache->data, FIELD_GET(PVT_DATA_DATA_MASK, val));
 457
 458	complete(&cache->conversion);
 459
 460	return IRQ_HANDLED;
 461}
 462
 463#define pvt_soft_isr NULL
 464
 465static inline umode_t pvt_limit_is_visible(enum pvt_sensor_type type)
 466{
 467	return 0;
 468}
 469
 470static inline umode_t pvt_alarm_is_visible(enum pvt_sensor_type type)
 471{
 472	return 0;
 473}
 474
 475static int pvt_read_data(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
 476			 long *val)
 477{
 478	struct pvt_cache *cache = &pvt->cache[type];
 
 479	u32 data;
 480	int ret;
 481
 482	/*
 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.
 487	 */
 488	ret = mutex_lock_interruptible(&pvt->iface_mtx);
 489	if (ret)
 490		return ret;
 491
 492	pvt->sensor = type;
 493	pvt_set_mode(pvt, pvt_info[type].mode);
 494
 495	/*
 496	 * Unmask the DVALID interrupt and enable the sensors conversions.
 497	 * Do the reverse procedure when conversion is done.
 498	 */
 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);
 501
 502	wait_for_completion(&cache->conversion);
 
 
 
 
 
 
 
 503
 504	pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
 505	pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID,
 506		   PVT_INTR_DVALID);
 507
 508	data = READ_ONCE(cache->data);
 509
 510	mutex_unlock(&pvt->iface_mtx);
 511
 
 
 
 512	if (type == PVT_TEMP)
 513		*val = pvt_calc_poly(&poly_N_to_temp, data);
 514	else
 515		*val = pvt_calc_poly(&poly_N_to_volt, data);
 516
 517	return 0;
 518}
 519
 520static int pvt_read_limit(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
 521			  bool is_low, long *val)
 522{
 523	return -EOPNOTSUPP;
 524}
 525
 526static int pvt_write_limit(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
 527			   bool is_low, long val)
 528{
 529	return -EOPNOTSUPP;
 530}
 531
 532static int pvt_read_alarm(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
 533			  bool is_low, long *val)
 534{
 535	return -EOPNOTSUPP;
 536}
 537
 538static 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 |
 543			   HWMON_T_OFFSET),
 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),
 549	NULL
 550};
 551
 552#endif /* !CONFIG_SENSORS_BT1_PVT_ALARMS */
 553
 554static inline bool pvt_hwmon_channel_is_valid(enum hwmon_sensor_types type,
 555					      int ch)
 556{
 557	switch (type) {
 558	case hwmon_temp:
 559		if (ch < 0 || ch >= PVT_TEMP_CHS)
 560			return false;
 561		break;
 562	case hwmon_in:
 563		if (ch < 0 || ch >= PVT_VOLT_CHS)
 564			return false;
 565		break;
 566	default:
 567		break;
 568	}
 569
 570	/* The rest of the types are independent from the channel number. */
 571	return true;
 572}
 573
 574static umode_t pvt_hwmon_is_visible(const void *data,
 575				    enum hwmon_sensor_types type,
 576				    u32 attr, int ch)
 577{
 578	if (!pvt_hwmon_channel_is_valid(type, ch))
 579		return 0;
 580
 581	switch (type) {
 582	case hwmon_chip:
 583		switch (attr) {
 584		case hwmon_chip_update_interval:
 585			return 0644;
 586		}
 587		break;
 588	case hwmon_temp:
 589		switch (attr) {
 590		case hwmon_temp_input:
 591		case hwmon_temp_type:
 592		case hwmon_temp_label:
 593			return 0444;
 594		case hwmon_temp_min:
 595		case hwmon_temp_max:
 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:
 601			return 0644;
 602		}
 603		break;
 604	case hwmon_in:
 605		switch (attr) {
 606		case hwmon_in_input:
 607		case hwmon_in_label:
 608			return 0444;
 609		case hwmon_in_min:
 610		case hwmon_in_max:
 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);
 615		}
 616		break;
 617	default:
 618		break;
 619	}
 620
 621	return 0;
 622}
 623
 624static int pvt_read_trim(struct pvt_hwmon *pvt, long *val)
 625{
 626	u32 data;
 627
 628	data = readl(pvt->regs + PVT_CTRL);
 629	*val = FIELD_GET(PVT_CTRL_TRIM_MASK, data) * PVT_TRIM_STEP;
 630
 631	return 0;
 632}
 633
 634static int pvt_write_trim(struct pvt_hwmon *pvt, long val)
 635{
 636	u32 trim;
 637	int ret;
 638
 639	/*
 640	 * Serialize trim update, since a part of the register is changed and
 641	 * the controller is supposed to be disabled during this operation.
 642	 */
 643	ret = mutex_lock_interruptible(&pvt->iface_mtx);
 644	if (ret)
 645		return ret;
 646
 647	trim = pvt_calc_trim(val);
 648	pvt_set_trim(pvt, trim);
 649
 650	mutex_unlock(&pvt->iface_mtx);
 651
 652	return 0;
 653}
 654
 655static int pvt_read_timeout(struct pvt_hwmon *pvt, long *val)
 656{
 657	unsigned long rate;
 658	ktime_t kt;
 659	u32 data;
 660
 661	rate = clk_get_rate(pvt->clks[PVT_CLOCK_REF].clk);
 662	if (!rate)
 663		return -ENODEV;
 664
 665	/*
 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.
 669	 */
 670	data = readl(pvt->regs + PVT_TTIMEOUT) + 1;
 671
 672	/*
 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.
 682	 */
 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);
 687#else
 688	kt = ktime_set(data, 0);
 689	kt = ktime_divns(kt, rate);
 690	kt = ktime_add_ns(kt, PVT_TOUT_MIN);
 691#endif
 692
 693	/* Return the result in msec as hwmon sysfs interface requires. */
 694	*val = ktime_to_ms(kt);
 695
 696	return 0;
 697}
 698
 699static int pvt_write_timeout(struct pvt_hwmon *pvt, long val)
 700{
 701	unsigned long rate;
 702	ktime_t kt;
 703	u32 data;
 704	int ret;
 705
 706	rate = clk_get_rate(pvt->clks[PVT_CLOCK_REF].clk);
 707	if (!rate)
 708		return -ENODEV;
 709
 710	/*
 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.
 714	 */
 715	kt = ms_to_ktime(val);
 716#if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
 717	kt = ktime_divns(kt, PVT_SENSORS_NUM);
 718#endif
 719
 720	/*
 721	 * Subtract a constant lag, which always persists due to the limited
 722	 * PVT sampling rate. Make sure the timeout is not negative.
 723	 */
 724	kt = ktime_sub_ns(kt, PVT_TOUT_MIN);
 725	if (ktime_to_ns(kt) < 0)
 726		kt = ktime_set(0, 0);
 727
 728	/*
 729	 * Finally recalculate the timeout in terms of the reference clock
 730	 * period.
 731	 */
 732	data = ktime_divns(kt * rate, NSEC_PER_SEC);
 733
 734	/*
 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
 737	 * updated.
 738	 */
 739	ret = mutex_lock_interruptible(&pvt->iface_mtx);
 740	if (ret)
 741		return ret;
 742
 743	pvt_set_tout(pvt, data);
 
 744
 745	mutex_unlock(&pvt->iface_mtx);
 746
 747	return 0;
 748}
 749
 750static int pvt_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
 751			  u32 attr, int ch, long *val)
 752{
 753	struct pvt_hwmon *pvt = dev_get_drvdata(dev);
 754
 755	if (!pvt_hwmon_channel_is_valid(type, ch))
 756		return -EINVAL;
 757
 758	switch (type) {
 759	case hwmon_chip:
 760		switch (attr) {
 761		case hwmon_chip_update_interval:
 762			return pvt_read_timeout(pvt, val);
 763		}
 764		break;
 765	case hwmon_temp:
 766		switch (attr) {
 767		case hwmon_temp_input:
 768			return pvt_read_data(pvt, ch, val);
 769		case hwmon_temp_type:
 770			*val = 1;
 771			return 0;
 772		case hwmon_temp_min:
 773			return pvt_read_limit(pvt, ch, true, val);
 774		case hwmon_temp_max:
 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);
 782		}
 783		break;
 784	case hwmon_in:
 785		switch (attr) {
 786		case hwmon_in_input:
 787			return pvt_read_data(pvt, PVT_VOLT + ch, val);
 788		case hwmon_in_min:
 789			return pvt_read_limit(pvt, PVT_VOLT + ch, true, val);
 790		case hwmon_in_max:
 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);
 796		}
 797		break;
 798	default:
 799		break;
 800	}
 801
 802	return -EOPNOTSUPP;
 803}
 804
 805static int pvt_hwmon_read_string(struct device *dev,
 806				 enum hwmon_sensor_types type,
 807				 u32 attr, int ch, const char **str)
 808{
 809	if (!pvt_hwmon_channel_is_valid(type, ch))
 810		return -EINVAL;
 811
 812	switch (type) {
 813	case hwmon_temp:
 814		switch (attr) {
 815		case hwmon_temp_label:
 816			*str = pvt_info[ch].label;
 817			return 0;
 818		}
 819		break;
 820	case hwmon_in:
 821		switch (attr) {
 822		case hwmon_in_label:
 823			*str = pvt_info[PVT_VOLT + ch].label;
 824			return 0;
 825		}
 826		break;
 827	default:
 828		break;
 829	}
 830
 831	return -EOPNOTSUPP;
 832}
 833
 834static int pvt_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
 835			   u32 attr, int ch, long val)
 836{
 837	struct pvt_hwmon *pvt = dev_get_drvdata(dev);
 838
 839	if (!pvt_hwmon_channel_is_valid(type, ch))
 840		return -EINVAL;
 841
 842	switch (type) {
 843	case hwmon_chip:
 844		switch (attr) {
 845		case hwmon_chip_update_interval:
 846			return pvt_write_timeout(pvt, val);
 847		}
 848		break;
 849	case hwmon_temp:
 850		switch (attr) {
 851		case hwmon_temp_min:
 852			return pvt_write_limit(pvt, ch, true, val);
 853		case hwmon_temp_max:
 854			return pvt_write_limit(pvt, ch, false, val);
 855		case hwmon_temp_offset:
 856			return pvt_write_trim(pvt, val);
 857		}
 858		break;
 859	case hwmon_in:
 860		switch (attr) {
 861		case hwmon_in_min:
 862			return pvt_write_limit(pvt, PVT_VOLT + ch, true, val);
 863		case hwmon_in_max:
 864			return pvt_write_limit(pvt, PVT_VOLT + ch, false, val);
 865		}
 866		break;
 867	default:
 868		break;
 869	}
 870
 871	return -EOPNOTSUPP;
 872}
 873
 874static 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
 879};
 880
 881static const struct hwmon_chip_info pvt_hwmon_info = {
 882	.ops = &pvt_hwmon_ops,
 883	.info = pvt_channel_info
 884};
 885
 886static void pvt_clear_data(void *data)
 887{
 888	struct pvt_hwmon *pvt = data;
 889#if !defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
 890	int idx;
 891
 892	for (idx = 0; idx < PVT_SENSORS_NUM; ++idx)
 893		complete_all(&pvt->cache[idx].conversion);
 894#endif
 895
 896	mutex_destroy(&pvt->iface_mtx);
 897}
 898
 899static struct pvt_hwmon *pvt_create_data(struct platform_device *pdev)
 900{
 901	struct device *dev = &pdev->dev;
 902	struct pvt_hwmon *pvt;
 903	int ret, idx;
 904
 905	pvt = devm_kzalloc(dev, sizeof(*pvt), GFP_KERNEL);
 906	if (!pvt)
 907		return ERR_PTR(-ENOMEM);
 908
 909	ret = devm_add_action(dev, pvt_clear_data, pvt);
 910	if (ret) {
 911		dev_err(dev, "Can't add PVT data clear action\n");
 912		return ERR_PTR(ret);
 913	}
 914
 915	pvt->dev = dev;
 916	pvt->sensor = PVT_SENSOR_FIRST;
 917	mutex_init(&pvt->iface_mtx);
 918
 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);
 922#else
 923	for (idx = 0; idx < PVT_SENSORS_NUM; ++idx)
 924		init_completion(&pvt->cache[idx].conversion);
 925#endif
 926
 927	return pvt;
 928}
 929
 930static int pvt_request_regs(struct pvt_hwmon *pvt)
 931{
 932	struct platform_device *pdev = to_platform_device(pvt->dev);
 933	struct resource *res;
 934
 935	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 936	if (!res) {
 937		dev_err(pvt->dev, "Couldn't find PVT memresource\n");
 938		return -EINVAL;
 939	}
 940
 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);
 945	}
 946
 947	return 0;
 948}
 949
 950static void pvt_disable_clks(void *data)
 951{
 952	struct pvt_hwmon *pvt = data;
 953
 954	clk_bulk_disable_unprepare(PVT_CLOCK_NUM, pvt->clks);
 955}
 956
 957static int pvt_request_clks(struct pvt_hwmon *pvt)
 958{
 959	int ret;
 960
 961	pvt->clks[PVT_CLOCK_APB].id = "pclk";
 962	pvt->clks[PVT_CLOCK_REF].id = "ref";
 963
 964	ret = devm_clk_bulk_get(pvt->dev, PVT_CLOCK_NUM, pvt->clks);
 965	if (ret) {
 966		dev_err(pvt->dev, "Couldn't get PVT clocks descriptors\n");
 967		return ret;
 968	}
 969
 970	ret = clk_bulk_prepare_enable(PVT_CLOCK_NUM, pvt->clks);
 971	if (ret) {
 972		dev_err(pvt->dev, "Couldn't enable the PVT clocks\n");
 973		return ret;
 974	}
 975
 976	ret = devm_add_action_or_reset(pvt->dev, pvt_disable_clks, pvt);
 977	if (ret) {
 978		dev_err(pvt->dev, "Can't add PVT clocks disable action\n");
 979		return ret;
 980	}
 981
 982	return 0;
 983}
 984
 985static void pvt_init_iface(struct pvt_hwmon *pvt)
 986{
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 987	u32 trim, temp;
 988
 
 
 
 
 
 
 989	/*
 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.
 993	 */
 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);
 998
 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);
1002
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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);
1007
1008	pvt_set_trim(pvt, trim);
 
 
1009}
1010
1011static int pvt_request_irq(struct pvt_hwmon *pvt)
1012{
1013	struct platform_device *pdev = to_platform_device(pvt->dev);
1014	int ret;
1015
1016	pvt->irq = platform_get_irq(pdev, 0);
1017	if (pvt->irq < 0)
1018		return pvt->irq;
1019
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 |
1024					IRQF_ONESHOT,
1025#else
1026					IRQF_SHARED | IRQF_TRIGGER_HIGH,
1027#endif
1028					"pvt", pvt);
1029	if (ret) {
1030		dev_err(pvt->dev, "Couldn't request PVT IRQ\n");
1031		return ret;
1032	}
1033
1034	return 0;
1035}
1036
1037static int pvt_create_hwmon(struct pvt_hwmon *pvt)
1038{
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);
1044	}
1045
1046	return 0;
1047}
1048
1049#if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
1050
1051static void pvt_disable_iface(void *data)
1052{
1053	struct pvt_hwmon *pvt = data;
1054
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,
1058		   PVT_INTR_DVALID);
1059	mutex_unlock(&pvt->iface_mtx);
1060}
1061
1062static int pvt_enable_iface(struct pvt_hwmon *pvt)
1063{
1064	int ret;
1065
1066	ret = devm_add_action(pvt->dev, pvt_disable_iface, pvt);
1067	if (ret) {
1068		dev_err(pvt->dev, "Can't add PVT disable interface action\n");
1069		return ret;
1070	}
1071
1072	/*
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.
1077	 */
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);
1082
1083	return 0;
1084}
1085
1086#else /* !CONFIG_SENSORS_BT1_PVT_ALARMS */
1087
1088static int pvt_enable_iface(struct pvt_hwmon *pvt)
1089{
1090	return 0;
1091}
1092
1093#endif /* !CONFIG_SENSORS_BT1_PVT_ALARMS */
1094
1095static int pvt_probe(struct platform_device *pdev)
1096{
1097	struct pvt_hwmon *pvt;
1098	int ret;
1099
1100	pvt = pvt_create_data(pdev);
1101	if (IS_ERR(pvt))
1102		return PTR_ERR(pvt);
1103
1104	ret = pvt_request_regs(pvt);
1105	if (ret)
1106		return ret;
1107
1108	ret = pvt_request_clks(pvt);
1109	if (ret)
1110		return ret;
1111
1112	pvt_init_iface(pvt);
 
 
 
 
 
 
1113
1114	ret = pvt_request_irq(pvt);
1115	if (ret)
1116		return ret;
1117
1118	ret = pvt_create_hwmon(pvt);
1119	if (ret)
1120		return ret;
1121
1122	ret = pvt_enable_iface(pvt);
1123	if (ret)
1124		return ret;
1125
1126	return 0;
1127}
1128
1129static const struct of_device_id pvt_of_match[] = {
1130	{ .compatible = "baikal,bt1-pvt" },
1131	{ }
1132};
1133MODULE_DEVICE_TABLE(of, pvt_of_match);
1134
1135static struct platform_driver pvt_driver = {
1136	.probe = pvt_probe,
1137	.driver = {
1138		.name = "bt1-pvt",
1139		.of_match_table = pvt_of_match
1140	}
1141};
1142module_platform_driver(pvt_driver);
1143
1144MODULE_AUTHOR("Maxim Kaurkin <maxim.kaurkin@baikalelectronics.ru>");
1145MODULE_DESCRIPTION("Baikal-T1 PVT driver");
1146MODULE_LICENSE("GPL v2");