0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <linux/bitfield.h>
0013 #include <linux/bitops.h>
0014 #include <linux/clk.h>
0015 #include <linux/completion.h>
0016 #include <linux/delay.h>
0017 #include <linux/device.h>
0018 #include <linux/hwmon-sysfs.h>
0019 #include <linux/hwmon.h>
0020 #include <linux/interrupt.h>
0021 #include <linux/io.h>
0022 #include <linux/kernel.h>
0023 #include <linux/ktime.h>
0024 #include <linux/limits.h>
0025 #include <linux/module.h>
0026 #include <linux/mutex.h>
0027 #include <linux/of.h>
0028 #include <linux/platform_device.h>
0029 #include <linux/polynomial.h>
0030 #include <linux/seqlock.h>
0031 #include <linux/sysfs.h>
0032 #include <linux/types.h>
0033
0034 #include "bt1-pvt.h"
0035
0036
0037
0038
0039
0040
0041 static const struct pvt_sensor_info pvt_info[] = {
0042 PVT_SENSOR_INFO(0, "CPU Core Temperature", hwmon_temp, TEMP, TTHRES),
0043 PVT_SENSOR_INFO(0, "CPU Core Voltage", hwmon_in, VOLT, VTHRES),
0044 PVT_SENSOR_INFO(1, "CPU Core Low-Vt", hwmon_in, LVT, LTHRES),
0045 PVT_SENSOR_INFO(2, "CPU Core High-Vt", hwmon_in, HVT, HTHRES),
0046 PVT_SENSOR_INFO(3, "CPU Core Standard-Vt", hwmon_in, SVT, STHRES),
0047 };
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069 static const struct polynomial __maybe_unused poly_temp_to_N = {
0070 .total_divider = 10000,
0071 .terms = {
0072 {4, 18322, 10000, 10000},
0073 {3, 2343, 10000, 10},
0074 {2, 87018, 10000, 10},
0075 {1, 39269, 1000, 1},
0076 {0, 1720400, 1, 1}
0077 }
0078 };
0079
0080 static const struct polynomial poly_N_to_temp = {
0081 .total_divider = 1,
0082 .terms = {
0083 {4, -16743, 1000, 1},
0084 {3, 81542, 1000, 1},
0085 {2, -182010, 1000, 1},
0086 {1, 310200, 1000, 1},
0087 {0, -48380, 1, 1}
0088 }
0089 };
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101 static const struct polynomial __maybe_unused poly_volt_to_N = {
0102 .total_divider = 10,
0103 .terms = {
0104 {1, 18658, 1000, 1},
0105 {0, -11572, 1, 1}
0106 }
0107 };
0108
0109 static const struct polynomial poly_N_to_volt = {
0110 .total_divider = 10,
0111 .terms = {
0112 {1, 100000, 18658, 1},
0113 {0, 115720000, 1, 18658}
0114 }
0115 };
0116
0117 static inline u32 pvt_update(void __iomem *reg, u32 mask, u32 data)
0118 {
0119 u32 old;
0120
0121 old = readl_relaxed(reg);
0122 writel((old & ~mask) | (data & mask), reg);
0123
0124 return old & mask;
0125 }
0126
0127
0128
0129
0130
0131
0132
0133
0134 static inline void pvt_set_mode(struct pvt_hwmon *pvt, u32 mode)
0135 {
0136 u32 old;
0137
0138 mode = FIELD_PREP(PVT_CTRL_MODE_MASK, mode);
0139
0140 old = pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
0141 pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_MODE_MASK | PVT_CTRL_EN,
0142 mode | old);
0143 }
0144
0145 static inline u32 pvt_calc_trim(long temp)
0146 {
0147 temp = clamp_val(temp, 0, PVT_TRIM_TEMP);
0148
0149 return DIV_ROUND_UP(temp, PVT_TRIM_STEP);
0150 }
0151
0152 static inline void pvt_set_trim(struct pvt_hwmon *pvt, u32 trim)
0153 {
0154 u32 old;
0155
0156 trim = FIELD_PREP(PVT_CTRL_TRIM_MASK, trim);
0157
0158 old = pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
0159 pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_TRIM_MASK | PVT_CTRL_EN,
0160 trim | old);
0161 }
0162
0163 static inline void pvt_set_tout(struct pvt_hwmon *pvt, u32 tout)
0164 {
0165 u32 old;
0166
0167 old = pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
0168 writel(tout, pvt->regs + PVT_TTIMEOUT);
0169 pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, old);
0170 }
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200 #if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
0201
0202 #define pvt_hard_isr NULL
0203
0204 static irqreturn_t pvt_soft_isr(int irq, void *data)
0205 {
0206 const struct pvt_sensor_info *info;
0207 struct pvt_hwmon *pvt = data;
0208 struct pvt_cache *cache;
0209 u32 val, thres_sts, old;
0210
0211
0212
0213
0214
0215
0216 thres_sts = readl(pvt->regs + PVT_RAW_INTR_STAT);
0217
0218
0219
0220
0221
0222
0223 cache = &pvt->cache[pvt->sensor];
0224 info = &pvt_info[pvt->sensor];
0225 pvt->sensor = (pvt->sensor == PVT_SENSOR_LAST) ?
0226 PVT_SENSOR_FIRST : (pvt->sensor + 1);
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237 mutex_lock(&pvt->iface_mtx);
0238
0239 old = pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID,
0240 PVT_INTR_DVALID);
0241
0242 val = readl(pvt->regs + PVT_DATA);
0243
0244 pvt_set_mode(pvt, pvt_info[pvt->sensor].mode);
0245
0246 pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID, old);
0247
0248 mutex_unlock(&pvt->iface_mtx);
0249
0250
0251
0252
0253
0254
0255 write_seqlock(&cache->data_seqlock);
0256
0257 cache->data = FIELD_GET(PVT_DATA_DATA_MASK, val);
0258
0259 write_sequnlock(&cache->data_seqlock);
0260
0261
0262
0263
0264
0265
0266
0267 if ((thres_sts & info->thres_sts_lo) ^ cache->thres_sts_lo) {
0268 WRITE_ONCE(cache->thres_sts_lo, thres_sts & info->thres_sts_lo);
0269 hwmon_notify_event(pvt->hwmon, info->type, info->attr_min_alarm,
0270 info->channel);
0271 } else if ((thres_sts & info->thres_sts_hi) ^ cache->thres_sts_hi) {
0272 WRITE_ONCE(cache->thres_sts_hi, thres_sts & info->thres_sts_hi);
0273 hwmon_notify_event(pvt->hwmon, info->type, info->attr_max_alarm,
0274 info->channel);
0275 }
0276
0277 return IRQ_HANDLED;
0278 }
0279
0280 static inline umode_t pvt_limit_is_visible(enum pvt_sensor_type type)
0281 {
0282 return 0644;
0283 }
0284
0285 static inline umode_t pvt_alarm_is_visible(enum pvt_sensor_type type)
0286 {
0287 return 0444;
0288 }
0289
0290 static int pvt_read_data(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
0291 long *val)
0292 {
0293 struct pvt_cache *cache = &pvt->cache[type];
0294 unsigned int seq;
0295 u32 data;
0296
0297 do {
0298 seq = read_seqbegin(&cache->data_seqlock);
0299 data = cache->data;
0300 } while (read_seqretry(&cache->data_seqlock, seq));
0301
0302 if (type == PVT_TEMP)
0303 *val = polynomial_calc(&poly_N_to_temp, data);
0304 else
0305 *val = polynomial_calc(&poly_N_to_volt, data);
0306
0307 return 0;
0308 }
0309
0310 static int pvt_read_limit(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
0311 bool is_low, long *val)
0312 {
0313 u32 data;
0314
0315
0316 data = readl(pvt->regs + pvt_info[type].thres_base);
0317
0318 if (is_low)
0319 data = FIELD_GET(PVT_THRES_LO_MASK, data);
0320 else
0321 data = FIELD_GET(PVT_THRES_HI_MASK, data);
0322
0323 if (type == PVT_TEMP)
0324 *val = polynomial_calc(&poly_N_to_temp, data);
0325 else
0326 *val = polynomial_calc(&poly_N_to_volt, data);
0327
0328 return 0;
0329 }
0330
0331 static int pvt_write_limit(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
0332 bool is_low, long val)
0333 {
0334 u32 data, limit, mask;
0335 int ret;
0336
0337 if (type == PVT_TEMP) {
0338 val = clamp(val, PVT_TEMP_MIN, PVT_TEMP_MAX);
0339 data = polynomial_calc(&poly_temp_to_N, val);
0340 } else {
0341 val = clamp(val, PVT_VOLT_MIN, PVT_VOLT_MAX);
0342 data = polynomial_calc(&poly_volt_to_N, val);
0343 }
0344
0345
0346 ret = mutex_lock_interruptible(&pvt->iface_mtx);
0347 if (ret)
0348 return ret;
0349
0350
0351 limit = readl(pvt->regs + pvt_info[type].thres_base);
0352 if (is_low) {
0353 limit = FIELD_GET(PVT_THRES_HI_MASK, limit);
0354 data = clamp_val(data, PVT_DATA_MIN, limit);
0355 data = FIELD_PREP(PVT_THRES_LO_MASK, data);
0356 mask = PVT_THRES_LO_MASK;
0357 } else {
0358 limit = FIELD_GET(PVT_THRES_LO_MASK, limit);
0359 data = clamp_val(data, limit, PVT_DATA_MAX);
0360 data = FIELD_PREP(PVT_THRES_HI_MASK, data);
0361 mask = PVT_THRES_HI_MASK;
0362 }
0363
0364 pvt_update(pvt->regs + pvt_info[type].thres_base, mask, data);
0365
0366 mutex_unlock(&pvt->iface_mtx);
0367
0368 return 0;
0369 }
0370
0371 static int pvt_read_alarm(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
0372 bool is_low, long *val)
0373 {
0374 if (is_low)
0375 *val = !!READ_ONCE(pvt->cache[type].thres_sts_lo);
0376 else
0377 *val = !!READ_ONCE(pvt->cache[type].thres_sts_hi);
0378
0379 return 0;
0380 }
0381
0382 static const struct hwmon_channel_info *pvt_channel_info[] = {
0383 HWMON_CHANNEL_INFO(chip,
0384 HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL),
0385 HWMON_CHANNEL_INFO(temp,
0386 HWMON_T_INPUT | HWMON_T_TYPE | HWMON_T_LABEL |
0387 HWMON_T_MIN | HWMON_T_MIN_ALARM |
0388 HWMON_T_MAX | HWMON_T_MAX_ALARM |
0389 HWMON_T_OFFSET),
0390 HWMON_CHANNEL_INFO(in,
0391 HWMON_I_INPUT | HWMON_I_LABEL |
0392 HWMON_I_MIN | HWMON_I_MIN_ALARM |
0393 HWMON_I_MAX | HWMON_I_MAX_ALARM,
0394 HWMON_I_INPUT | HWMON_I_LABEL |
0395 HWMON_I_MIN | HWMON_I_MIN_ALARM |
0396 HWMON_I_MAX | HWMON_I_MAX_ALARM,
0397 HWMON_I_INPUT | HWMON_I_LABEL |
0398 HWMON_I_MIN | HWMON_I_MIN_ALARM |
0399 HWMON_I_MAX | HWMON_I_MAX_ALARM,
0400 HWMON_I_INPUT | HWMON_I_LABEL |
0401 HWMON_I_MIN | HWMON_I_MIN_ALARM |
0402 HWMON_I_MAX | HWMON_I_MAX_ALARM),
0403 NULL
0404 };
0405
0406 #else
0407
0408 static irqreturn_t pvt_hard_isr(int irq, void *data)
0409 {
0410 struct pvt_hwmon *pvt = data;
0411 struct pvt_cache *cache;
0412 u32 val;
0413
0414
0415
0416
0417
0418 pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID,
0419 PVT_INTR_DVALID);
0420
0421
0422
0423
0424
0425 val = readl(pvt->regs + PVT_DATA);
0426 if (!(val & PVT_DATA_VALID)) {
0427 dev_err(pvt->dev, "Got IRQ when data isn't valid\n");
0428 return IRQ_HANDLED;
0429 }
0430
0431 cache = &pvt->cache[pvt->sensor];
0432
0433 WRITE_ONCE(cache->data, FIELD_GET(PVT_DATA_DATA_MASK, val));
0434
0435 complete(&cache->conversion);
0436
0437 return IRQ_HANDLED;
0438 }
0439
0440 #define pvt_soft_isr NULL
0441
0442 static inline umode_t pvt_limit_is_visible(enum pvt_sensor_type type)
0443 {
0444 return 0;
0445 }
0446
0447 static inline umode_t pvt_alarm_is_visible(enum pvt_sensor_type type)
0448 {
0449 return 0;
0450 }
0451
0452 static int pvt_read_data(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
0453 long *val)
0454 {
0455 struct pvt_cache *cache = &pvt->cache[type];
0456 unsigned long timeout;
0457 u32 data;
0458 int ret;
0459
0460
0461
0462
0463
0464
0465
0466 ret = mutex_lock_interruptible(&pvt->iface_mtx);
0467 if (ret)
0468 return ret;
0469
0470 pvt->sensor = type;
0471 pvt_set_mode(pvt, pvt_info[type].mode);
0472
0473
0474
0475
0476
0477 pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID, 0);
0478 pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, PVT_CTRL_EN);
0479
0480
0481
0482
0483
0484
0485
0486 timeout = 2 * usecs_to_jiffies(ktime_to_us(pvt->timeout));
0487 ret = wait_for_completion_timeout(&cache->conversion, timeout);
0488
0489 pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
0490 pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID,
0491 PVT_INTR_DVALID);
0492
0493 data = READ_ONCE(cache->data);
0494
0495 mutex_unlock(&pvt->iface_mtx);
0496
0497 if (!ret)
0498 return -ETIMEDOUT;
0499
0500 if (type == PVT_TEMP)
0501 *val = polynomial_calc(&poly_N_to_temp, data);
0502 else
0503 *val = polynomial_calc(&poly_N_to_volt, data);
0504
0505 return 0;
0506 }
0507
0508 static int pvt_read_limit(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
0509 bool is_low, long *val)
0510 {
0511 return -EOPNOTSUPP;
0512 }
0513
0514 static int pvt_write_limit(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
0515 bool is_low, long val)
0516 {
0517 return -EOPNOTSUPP;
0518 }
0519
0520 static int pvt_read_alarm(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
0521 bool is_low, long *val)
0522 {
0523 return -EOPNOTSUPP;
0524 }
0525
0526 static const struct hwmon_channel_info *pvt_channel_info[] = {
0527 HWMON_CHANNEL_INFO(chip,
0528 HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL),
0529 HWMON_CHANNEL_INFO(temp,
0530 HWMON_T_INPUT | HWMON_T_TYPE | HWMON_T_LABEL |
0531 HWMON_T_OFFSET),
0532 HWMON_CHANNEL_INFO(in,
0533 HWMON_I_INPUT | HWMON_I_LABEL,
0534 HWMON_I_INPUT | HWMON_I_LABEL,
0535 HWMON_I_INPUT | HWMON_I_LABEL,
0536 HWMON_I_INPUT | HWMON_I_LABEL),
0537 NULL
0538 };
0539
0540 #endif
0541
0542 static inline bool pvt_hwmon_channel_is_valid(enum hwmon_sensor_types type,
0543 int ch)
0544 {
0545 switch (type) {
0546 case hwmon_temp:
0547 if (ch < 0 || ch >= PVT_TEMP_CHS)
0548 return false;
0549 break;
0550 case hwmon_in:
0551 if (ch < 0 || ch >= PVT_VOLT_CHS)
0552 return false;
0553 break;
0554 default:
0555 break;
0556 }
0557
0558
0559 return true;
0560 }
0561
0562 static umode_t pvt_hwmon_is_visible(const void *data,
0563 enum hwmon_sensor_types type,
0564 u32 attr, int ch)
0565 {
0566 if (!pvt_hwmon_channel_is_valid(type, ch))
0567 return 0;
0568
0569 switch (type) {
0570 case hwmon_chip:
0571 switch (attr) {
0572 case hwmon_chip_update_interval:
0573 return 0644;
0574 }
0575 break;
0576 case hwmon_temp:
0577 switch (attr) {
0578 case hwmon_temp_input:
0579 case hwmon_temp_type:
0580 case hwmon_temp_label:
0581 return 0444;
0582 case hwmon_temp_min:
0583 case hwmon_temp_max:
0584 return pvt_limit_is_visible(ch);
0585 case hwmon_temp_min_alarm:
0586 case hwmon_temp_max_alarm:
0587 return pvt_alarm_is_visible(ch);
0588 case hwmon_temp_offset:
0589 return 0644;
0590 }
0591 break;
0592 case hwmon_in:
0593 switch (attr) {
0594 case hwmon_in_input:
0595 case hwmon_in_label:
0596 return 0444;
0597 case hwmon_in_min:
0598 case hwmon_in_max:
0599 return pvt_limit_is_visible(PVT_VOLT + ch);
0600 case hwmon_in_min_alarm:
0601 case hwmon_in_max_alarm:
0602 return pvt_alarm_is_visible(PVT_VOLT + ch);
0603 }
0604 break;
0605 default:
0606 break;
0607 }
0608
0609 return 0;
0610 }
0611
0612 static int pvt_read_trim(struct pvt_hwmon *pvt, long *val)
0613 {
0614 u32 data;
0615
0616 data = readl(pvt->regs + PVT_CTRL);
0617 *val = FIELD_GET(PVT_CTRL_TRIM_MASK, data) * PVT_TRIM_STEP;
0618
0619 return 0;
0620 }
0621
0622 static int pvt_write_trim(struct pvt_hwmon *pvt, long val)
0623 {
0624 u32 trim;
0625 int ret;
0626
0627
0628
0629
0630
0631 ret = mutex_lock_interruptible(&pvt->iface_mtx);
0632 if (ret)
0633 return ret;
0634
0635 trim = pvt_calc_trim(val);
0636 pvt_set_trim(pvt, trim);
0637
0638 mutex_unlock(&pvt->iface_mtx);
0639
0640 return 0;
0641 }
0642
0643 static int pvt_read_timeout(struct pvt_hwmon *pvt, long *val)
0644 {
0645 int ret;
0646
0647 ret = mutex_lock_interruptible(&pvt->iface_mtx);
0648 if (ret)
0649 return ret;
0650
0651
0652 *val = ktime_to_ms(pvt->timeout);
0653
0654 mutex_unlock(&pvt->iface_mtx);
0655
0656 return 0;
0657 }
0658
0659 static int pvt_write_timeout(struct pvt_hwmon *pvt, long val)
0660 {
0661 unsigned long rate;
0662 ktime_t kt, cache;
0663 u32 data;
0664 int ret;
0665
0666 rate = clk_get_rate(pvt->clks[PVT_CLOCK_REF].clk);
0667 if (!rate)
0668 return -ENODEV;
0669
0670
0671
0672
0673
0674
0675 cache = kt = ms_to_ktime(val);
0676 #if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
0677 kt = ktime_divns(kt, PVT_SENSORS_NUM);
0678 #endif
0679
0680
0681
0682
0683
0684 kt = ktime_sub_ns(kt, PVT_TOUT_MIN);
0685 if (ktime_to_ns(kt) < 0)
0686 kt = ktime_set(0, 0);
0687
0688
0689
0690
0691
0692 data = ktime_divns(kt * rate, NSEC_PER_SEC);
0693
0694
0695
0696
0697
0698
0699 ret = mutex_lock_interruptible(&pvt->iface_mtx);
0700 if (ret)
0701 return ret;
0702
0703 pvt_set_tout(pvt, data);
0704 pvt->timeout = cache;
0705
0706 mutex_unlock(&pvt->iface_mtx);
0707
0708 return 0;
0709 }
0710
0711 static int pvt_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
0712 u32 attr, int ch, long *val)
0713 {
0714 struct pvt_hwmon *pvt = dev_get_drvdata(dev);
0715
0716 if (!pvt_hwmon_channel_is_valid(type, ch))
0717 return -EINVAL;
0718
0719 switch (type) {
0720 case hwmon_chip:
0721 switch (attr) {
0722 case hwmon_chip_update_interval:
0723 return pvt_read_timeout(pvt, val);
0724 }
0725 break;
0726 case hwmon_temp:
0727 switch (attr) {
0728 case hwmon_temp_input:
0729 return pvt_read_data(pvt, ch, val);
0730 case hwmon_temp_type:
0731 *val = 1;
0732 return 0;
0733 case hwmon_temp_min:
0734 return pvt_read_limit(pvt, ch, true, val);
0735 case hwmon_temp_max:
0736 return pvt_read_limit(pvt, ch, false, val);
0737 case hwmon_temp_min_alarm:
0738 return pvt_read_alarm(pvt, ch, true, val);
0739 case hwmon_temp_max_alarm:
0740 return pvt_read_alarm(pvt, ch, false, val);
0741 case hwmon_temp_offset:
0742 return pvt_read_trim(pvt, val);
0743 }
0744 break;
0745 case hwmon_in:
0746 switch (attr) {
0747 case hwmon_in_input:
0748 return pvt_read_data(pvt, PVT_VOLT + ch, val);
0749 case hwmon_in_min:
0750 return pvt_read_limit(pvt, PVT_VOLT + ch, true, val);
0751 case hwmon_in_max:
0752 return pvt_read_limit(pvt, PVT_VOLT + ch, false, val);
0753 case hwmon_in_min_alarm:
0754 return pvt_read_alarm(pvt, PVT_VOLT + ch, true, val);
0755 case hwmon_in_max_alarm:
0756 return pvt_read_alarm(pvt, PVT_VOLT + ch, false, val);
0757 }
0758 break;
0759 default:
0760 break;
0761 }
0762
0763 return -EOPNOTSUPP;
0764 }
0765
0766 static int pvt_hwmon_read_string(struct device *dev,
0767 enum hwmon_sensor_types type,
0768 u32 attr, int ch, const char **str)
0769 {
0770 if (!pvt_hwmon_channel_is_valid(type, ch))
0771 return -EINVAL;
0772
0773 switch (type) {
0774 case hwmon_temp:
0775 switch (attr) {
0776 case hwmon_temp_label:
0777 *str = pvt_info[ch].label;
0778 return 0;
0779 }
0780 break;
0781 case hwmon_in:
0782 switch (attr) {
0783 case hwmon_in_label:
0784 *str = pvt_info[PVT_VOLT + ch].label;
0785 return 0;
0786 }
0787 break;
0788 default:
0789 break;
0790 }
0791
0792 return -EOPNOTSUPP;
0793 }
0794
0795 static int pvt_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
0796 u32 attr, int ch, long val)
0797 {
0798 struct pvt_hwmon *pvt = dev_get_drvdata(dev);
0799
0800 if (!pvt_hwmon_channel_is_valid(type, ch))
0801 return -EINVAL;
0802
0803 switch (type) {
0804 case hwmon_chip:
0805 switch (attr) {
0806 case hwmon_chip_update_interval:
0807 return pvt_write_timeout(pvt, val);
0808 }
0809 break;
0810 case hwmon_temp:
0811 switch (attr) {
0812 case hwmon_temp_min:
0813 return pvt_write_limit(pvt, ch, true, val);
0814 case hwmon_temp_max:
0815 return pvt_write_limit(pvt, ch, false, val);
0816 case hwmon_temp_offset:
0817 return pvt_write_trim(pvt, val);
0818 }
0819 break;
0820 case hwmon_in:
0821 switch (attr) {
0822 case hwmon_in_min:
0823 return pvt_write_limit(pvt, PVT_VOLT + ch, true, val);
0824 case hwmon_in_max:
0825 return pvt_write_limit(pvt, PVT_VOLT + ch, false, val);
0826 }
0827 break;
0828 default:
0829 break;
0830 }
0831
0832 return -EOPNOTSUPP;
0833 }
0834
0835 static const struct hwmon_ops pvt_hwmon_ops = {
0836 .is_visible = pvt_hwmon_is_visible,
0837 .read = pvt_hwmon_read,
0838 .read_string = pvt_hwmon_read_string,
0839 .write = pvt_hwmon_write
0840 };
0841
0842 static const struct hwmon_chip_info pvt_hwmon_info = {
0843 .ops = &pvt_hwmon_ops,
0844 .info = pvt_channel_info
0845 };
0846
0847 static void pvt_clear_data(void *data)
0848 {
0849 struct pvt_hwmon *pvt = data;
0850 #if !defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
0851 int idx;
0852
0853 for (idx = 0; idx < PVT_SENSORS_NUM; ++idx)
0854 complete_all(&pvt->cache[idx].conversion);
0855 #endif
0856
0857 mutex_destroy(&pvt->iface_mtx);
0858 }
0859
0860 static struct pvt_hwmon *pvt_create_data(struct platform_device *pdev)
0861 {
0862 struct device *dev = &pdev->dev;
0863 struct pvt_hwmon *pvt;
0864 int ret, idx;
0865
0866 pvt = devm_kzalloc(dev, sizeof(*pvt), GFP_KERNEL);
0867 if (!pvt)
0868 return ERR_PTR(-ENOMEM);
0869
0870 ret = devm_add_action(dev, pvt_clear_data, pvt);
0871 if (ret) {
0872 dev_err(dev, "Can't add PVT data clear action\n");
0873 return ERR_PTR(ret);
0874 }
0875
0876 pvt->dev = dev;
0877 pvt->sensor = PVT_SENSOR_FIRST;
0878 mutex_init(&pvt->iface_mtx);
0879
0880 #if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
0881 for (idx = 0; idx < PVT_SENSORS_NUM; ++idx)
0882 seqlock_init(&pvt->cache[idx].data_seqlock);
0883 #else
0884 for (idx = 0; idx < PVT_SENSORS_NUM; ++idx)
0885 init_completion(&pvt->cache[idx].conversion);
0886 #endif
0887
0888 return pvt;
0889 }
0890
0891 static int pvt_request_regs(struct pvt_hwmon *pvt)
0892 {
0893 struct platform_device *pdev = to_platform_device(pvt->dev);
0894 struct resource *res;
0895
0896 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0897 if (!res) {
0898 dev_err(pvt->dev, "Couldn't find PVT memresource\n");
0899 return -EINVAL;
0900 }
0901
0902 pvt->regs = devm_ioremap_resource(pvt->dev, res);
0903 if (IS_ERR(pvt->regs))
0904 return PTR_ERR(pvt->regs);
0905
0906 return 0;
0907 }
0908
0909 static void pvt_disable_clks(void *data)
0910 {
0911 struct pvt_hwmon *pvt = data;
0912
0913 clk_bulk_disable_unprepare(PVT_CLOCK_NUM, pvt->clks);
0914 }
0915
0916 static int pvt_request_clks(struct pvt_hwmon *pvt)
0917 {
0918 int ret;
0919
0920 pvt->clks[PVT_CLOCK_APB].id = "pclk";
0921 pvt->clks[PVT_CLOCK_REF].id = "ref";
0922
0923 ret = devm_clk_bulk_get(pvt->dev, PVT_CLOCK_NUM, pvt->clks);
0924 if (ret) {
0925 dev_err(pvt->dev, "Couldn't get PVT clocks descriptors\n");
0926 return ret;
0927 }
0928
0929 ret = clk_bulk_prepare_enable(PVT_CLOCK_NUM, pvt->clks);
0930 if (ret) {
0931 dev_err(pvt->dev, "Couldn't enable the PVT clocks\n");
0932 return ret;
0933 }
0934
0935 ret = devm_add_action_or_reset(pvt->dev, pvt_disable_clks, pvt);
0936 if (ret) {
0937 dev_err(pvt->dev, "Can't add PVT clocks disable action\n");
0938 return ret;
0939 }
0940
0941 return 0;
0942 }
0943
0944 static int pvt_check_pwr(struct pvt_hwmon *pvt)
0945 {
0946 unsigned long tout;
0947 int ret = 0;
0948 u32 data;
0949
0950
0951
0952
0953
0954
0955
0956
0957
0958
0959
0960 pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_ALL, PVT_INTR_ALL);
0961 pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, PVT_CTRL_EN);
0962 pvt_set_tout(pvt, 0);
0963 readl(pvt->regs + PVT_DATA);
0964
0965 tout = PVT_TOUT_MIN / NSEC_PER_USEC;
0966 usleep_range(tout, 2 * tout);
0967
0968 data = readl(pvt->regs + PVT_DATA);
0969 if (!(data & PVT_DATA_VALID)) {
0970 ret = -ENODEV;
0971 dev_err(pvt->dev, "Sensor is powered down\n");
0972 }
0973
0974 pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
0975
0976 return ret;
0977 }
0978
0979 static int pvt_init_iface(struct pvt_hwmon *pvt)
0980 {
0981 unsigned long rate;
0982 u32 trim, temp;
0983
0984 rate = clk_get_rate(pvt->clks[PVT_CLOCK_REF].clk);
0985 if (!rate) {
0986 dev_err(pvt->dev, "Invalid reference clock rate\n");
0987 return -ENODEV;
0988 }
0989
0990
0991
0992
0993
0994
0995 pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_ALL, PVT_INTR_ALL);
0996 pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
0997 readl(pvt->regs + PVT_CLR_INTR);
0998 readl(pvt->regs + PVT_DATA);
0999
1000
1001 pvt_set_mode(pvt, pvt_info[pvt->sensor].mode);
1002 pvt_set_tout(pvt, PVT_TOUT_DEF);
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017 #if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
1018 pvt->timeout = ktime_set(PVT_SENSORS_NUM * PVT_TOUT_DEF, 0);
1019 pvt->timeout = ktime_divns(pvt->timeout, rate);
1020 pvt->timeout = ktime_add_ns(pvt->timeout, PVT_SENSORS_NUM * PVT_TOUT_MIN);
1021 #else
1022 pvt->timeout = ktime_set(PVT_TOUT_DEF, 0);
1023 pvt->timeout = ktime_divns(pvt->timeout, rate);
1024 pvt->timeout = ktime_add_ns(pvt->timeout, PVT_TOUT_MIN);
1025 #endif
1026
1027 trim = PVT_TRIM_DEF;
1028 if (!of_property_read_u32(pvt->dev->of_node,
1029 "baikal,pvt-temp-offset-millicelsius", &temp))
1030 trim = pvt_calc_trim(temp);
1031
1032 pvt_set_trim(pvt, trim);
1033
1034 return 0;
1035 }
1036
1037 static int pvt_request_irq(struct pvt_hwmon *pvt)
1038 {
1039 struct platform_device *pdev = to_platform_device(pvt->dev);
1040 int ret;
1041
1042 pvt->irq = platform_get_irq(pdev, 0);
1043 if (pvt->irq < 0)
1044 return pvt->irq;
1045
1046 ret = devm_request_threaded_irq(pvt->dev, pvt->irq,
1047 pvt_hard_isr, pvt_soft_isr,
1048 #if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
1049 IRQF_SHARED | IRQF_TRIGGER_HIGH |
1050 IRQF_ONESHOT,
1051 #else
1052 IRQF_SHARED | IRQF_TRIGGER_HIGH,
1053 #endif
1054 "pvt", pvt);
1055 if (ret) {
1056 dev_err(pvt->dev, "Couldn't request PVT IRQ\n");
1057 return ret;
1058 }
1059
1060 return 0;
1061 }
1062
1063 static int pvt_create_hwmon(struct pvt_hwmon *pvt)
1064 {
1065 pvt->hwmon = devm_hwmon_device_register_with_info(pvt->dev, "pvt", pvt,
1066 &pvt_hwmon_info, NULL);
1067 if (IS_ERR(pvt->hwmon)) {
1068 dev_err(pvt->dev, "Couldn't create hwmon device\n");
1069 return PTR_ERR(pvt->hwmon);
1070 }
1071
1072 return 0;
1073 }
1074
1075 #if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
1076
1077 static void pvt_disable_iface(void *data)
1078 {
1079 struct pvt_hwmon *pvt = data;
1080
1081 mutex_lock(&pvt->iface_mtx);
1082 pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
1083 pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID,
1084 PVT_INTR_DVALID);
1085 mutex_unlock(&pvt->iface_mtx);
1086 }
1087
1088 static int pvt_enable_iface(struct pvt_hwmon *pvt)
1089 {
1090 int ret;
1091
1092 ret = devm_add_action(pvt->dev, pvt_disable_iface, pvt);
1093 if (ret) {
1094 dev_err(pvt->dev, "Can't add PVT disable interface action\n");
1095 return ret;
1096 }
1097
1098
1099
1100
1101
1102
1103
1104 mutex_lock(&pvt->iface_mtx);
1105 pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID, 0);
1106 pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, PVT_CTRL_EN);
1107 mutex_unlock(&pvt->iface_mtx);
1108
1109 return 0;
1110 }
1111
1112 #else
1113
1114 static int pvt_enable_iface(struct pvt_hwmon *pvt)
1115 {
1116 return 0;
1117 }
1118
1119 #endif
1120
1121 static int pvt_probe(struct platform_device *pdev)
1122 {
1123 struct pvt_hwmon *pvt;
1124 int ret;
1125
1126 pvt = pvt_create_data(pdev);
1127 if (IS_ERR(pvt))
1128 return PTR_ERR(pvt);
1129
1130 ret = pvt_request_regs(pvt);
1131 if (ret)
1132 return ret;
1133
1134 ret = pvt_request_clks(pvt);
1135 if (ret)
1136 return ret;
1137
1138 ret = pvt_check_pwr(pvt);
1139 if (ret)
1140 return ret;
1141
1142 ret = pvt_init_iface(pvt);
1143 if (ret)
1144 return ret;
1145
1146 ret = pvt_request_irq(pvt);
1147 if (ret)
1148 return ret;
1149
1150 ret = pvt_create_hwmon(pvt);
1151 if (ret)
1152 return ret;
1153
1154 ret = pvt_enable_iface(pvt);
1155 if (ret)
1156 return ret;
1157
1158 return 0;
1159 }
1160
1161 static const struct of_device_id pvt_of_match[] = {
1162 { .compatible = "baikal,bt1-pvt" },
1163 { }
1164 };
1165 MODULE_DEVICE_TABLE(of, pvt_of_match);
1166
1167 static struct platform_driver pvt_driver = {
1168 .probe = pvt_probe,
1169 .driver = {
1170 .name = "bt1-pvt",
1171 .of_match_table = pvt_of_match
1172 }
1173 };
1174 module_platform_driver(pvt_driver);
1175
1176 MODULE_AUTHOR("Maxim Kaurkin <maxim.kaurkin@baikalelectronics.ru>");
1177 MODULE_DESCRIPTION("Baikal-T1 PVT driver");
1178 MODULE_LICENSE("GPL v2");