Lines Matching refs:pvt

34 #include "bt1-pvt.h"
134 static inline void pvt_set_mode(struct pvt_hwmon *pvt, u32 mode)
140 old = pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
141 pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_MODE_MASK | PVT_CTRL_EN,
152 static inline void pvt_set_trim(struct pvt_hwmon *pvt, u32 trim)
158 old = pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
159 pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_TRIM_MASK | PVT_CTRL_EN,
163 static inline void pvt_set_tout(struct pvt_hwmon *pvt, u32 tout)
167 old = pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
168 writel(tout, pvt->regs + PVT_TTIMEOUT);
169 pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, old);
207 struct pvt_hwmon *pvt = data;
216 thres_sts = readl(pvt->regs + PVT_RAW_INTR_STAT);
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);
237 mutex_lock(&pvt->iface_mtx);
239 old = pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID,
242 val = readl(pvt->regs + PVT_DATA);
244 pvt_set_mode(pvt, pvt_info[pvt->sensor].mode);
246 pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID, old);
248 mutex_unlock(&pvt->iface_mtx);
269 hwmon_notify_event(pvt->hwmon, info->type, info->attr_min_alarm,
273 hwmon_notify_event(pvt->hwmon, info->type, info->attr_max_alarm,
290 static int pvt_read_data(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
293 struct pvt_cache *cache = &pvt->cache[type];
310 static int pvt_read_limit(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
316 data = readl(pvt->regs + pvt_info[type].thres_base);
331 static int pvt_write_limit(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
346 ret = mutex_lock_interruptible(&pvt->iface_mtx);
351 limit = readl(pvt->regs + pvt_info[type].thres_base);
364 pvt_update(pvt->regs + pvt_info[type].thres_base, mask, data);
366 mutex_unlock(&pvt->iface_mtx);
371 static int pvt_read_alarm(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
375 *val = !!READ_ONCE(pvt->cache[type].thres_sts_lo);
377 *val = !!READ_ONCE(pvt->cache[type].thres_sts_hi);
410 struct pvt_hwmon *pvt = data;
418 pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID,
425 val = readl(pvt->regs + PVT_DATA);
427 dev_err(pvt->dev, "Got IRQ when data isn't valid\n");
431 cache = &pvt->cache[pvt->sensor];
452 static int pvt_read_data(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
455 struct pvt_cache *cache = &pvt->cache[type];
466 ret = mutex_lock_interruptible(&pvt->iface_mtx);
470 pvt->sensor = type;
471 pvt_set_mode(pvt, pvt_info[type].mode);
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);
486 timeout = 2 * usecs_to_jiffies(ktime_to_us(pvt->timeout));
489 pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
490 pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID,
495 mutex_unlock(&pvt->iface_mtx);
508 static int pvt_read_limit(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
514 static int pvt_write_limit(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
520 static int pvt_read_alarm(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
612 static int pvt_read_trim(struct pvt_hwmon *pvt, long *val)
616 data = readl(pvt->regs + PVT_CTRL);
622 static int pvt_write_trim(struct pvt_hwmon *pvt, long val)
631 ret = mutex_lock_interruptible(&pvt->iface_mtx);
636 pvt_set_trim(pvt, trim);
638 mutex_unlock(&pvt->iface_mtx);
643 static int pvt_read_timeout(struct pvt_hwmon *pvt, long *val)
647 ret = mutex_lock_interruptible(&pvt->iface_mtx);
652 *val = ktime_to_ms(pvt->timeout);
654 mutex_unlock(&pvt->iface_mtx);
659 static int pvt_write_timeout(struct pvt_hwmon *pvt, long val)
666 rate = clk_get_rate(pvt->clks[PVT_CLOCK_REF].clk);
699 ret = mutex_lock_interruptible(&pvt->iface_mtx);
703 pvt_set_tout(pvt, data);
704 pvt->timeout = cache;
706 mutex_unlock(&pvt->iface_mtx);
714 struct pvt_hwmon *pvt = dev_get_drvdata(dev);
723 return pvt_read_timeout(pvt, val);
729 return pvt_read_data(pvt, ch, val);
734 return pvt_read_limit(pvt, ch, true, val);
736 return pvt_read_limit(pvt, ch, false, val);
738 return pvt_read_alarm(pvt, ch, true, val);
740 return pvt_read_alarm(pvt, ch, false, val);
742 return pvt_read_trim(pvt, val);
748 return pvt_read_data(pvt, PVT_VOLT + ch, val);
750 return pvt_read_limit(pvt, PVT_VOLT + ch, true, val);
752 return pvt_read_limit(pvt, PVT_VOLT + ch, false, val);
754 return pvt_read_alarm(pvt, PVT_VOLT + ch, true, val);
756 return pvt_read_alarm(pvt, PVT_VOLT + ch, false, val);
798 struct pvt_hwmon *pvt = dev_get_drvdata(dev);
807 return pvt_write_timeout(pvt, val);
813 return pvt_write_limit(pvt, ch, true, val);
815 return pvt_write_limit(pvt, ch, false, val);
817 return pvt_write_trim(pvt, val);
823 return pvt_write_limit(pvt, PVT_VOLT + ch, true, val);
825 return pvt_write_limit(pvt, PVT_VOLT + ch, false, val);
849 struct pvt_hwmon *pvt = data;
854 complete_all(&pvt->cache[idx].conversion);
857 mutex_destroy(&pvt->iface_mtx);
863 struct pvt_hwmon *pvt;
866 pvt = devm_kzalloc(dev, sizeof(*pvt), GFP_KERNEL);
867 if (!pvt)
870 ret = devm_add_action(dev, pvt_clear_data, pvt);
876 pvt->dev = dev;
877 pvt->sensor = PVT_SENSOR_FIRST;
878 mutex_init(&pvt->iface_mtx);
882 seqlock_init(&pvt->cache[idx].data_seqlock);
885 init_completion(&pvt->cache[idx].conversion);
888 return pvt;
891 static int pvt_request_regs(struct pvt_hwmon *pvt)
893 struct platform_device *pdev = to_platform_device(pvt->dev);
895 pvt->regs = devm_platform_ioremap_resource(pdev, 0);
896 if (IS_ERR(pvt->regs))
897 return PTR_ERR(pvt->regs);
904 struct pvt_hwmon *pvt = data;
906 clk_bulk_disable_unprepare(PVT_CLOCK_NUM, pvt->clks);
909 static int pvt_request_clks(struct pvt_hwmon *pvt)
913 pvt->clks[PVT_CLOCK_APB].id = "pclk";
914 pvt->clks[PVT_CLOCK_REF].id = "ref";
916 ret = devm_clk_bulk_get(pvt->dev, PVT_CLOCK_NUM, pvt->clks);
918 dev_err(pvt->dev, "Couldn't get PVT clocks descriptors\n");
922 ret = clk_bulk_prepare_enable(PVT_CLOCK_NUM, pvt->clks);
924 dev_err(pvt->dev, "Couldn't enable the PVT clocks\n");
928 ret = devm_add_action_or_reset(pvt->dev, pvt_disable_clks, pvt);
930 dev_err(pvt->dev, "Can't add PVT clocks disable action\n");
937 static int pvt_check_pwr(struct pvt_hwmon *pvt)
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);
961 data = readl(pvt->regs + PVT_DATA);
964 dev_err(pvt->dev, "Sensor is powered down\n");
967 pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
972 static int pvt_init_iface(struct pvt_hwmon *pvt)
977 rate = clk_get_rate(pvt->clks[PVT_CLOCK_REF].clk);
979 dev_err(pvt->dev, "Invalid reference clock rate\n");
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);
994 pvt_set_mode(pvt, pvt_info[pvt->sensor].mode);
995 pvt_set_tout(pvt, PVT_TOUT_DEF);
1011 pvt->timeout = ktime_set(PVT_SENSORS_NUM * PVT_TOUT_DEF, 0);
1012 pvt->timeout = ktime_divns(pvt->timeout, rate);
1013 pvt->timeout = ktime_add_ns(pvt->timeout, PVT_SENSORS_NUM * PVT_TOUT_MIN);
1015 pvt->timeout = ktime_set(PVT_TOUT_DEF, 0);
1016 pvt->timeout = ktime_divns(pvt->timeout, rate);
1017 pvt->timeout = ktime_add_ns(pvt->timeout, PVT_TOUT_MIN);
1021 if (!of_property_read_u32(pvt->dev->of_node,
1022 "baikal,pvt-temp-offset-millicelsius", &temp))
1025 pvt_set_trim(pvt, trim);
1030 static int pvt_request_irq(struct pvt_hwmon *pvt)
1032 struct platform_device *pdev = to_platform_device(pvt->dev);
1035 pvt->irq = platform_get_irq(pdev, 0);
1036 if (pvt->irq < 0)
1037 return pvt->irq;
1039 ret = devm_request_threaded_irq(pvt->dev, pvt->irq,
1047 "pvt", pvt);
1049 dev_err(pvt->dev, "Couldn't request PVT IRQ\n");
1056 static int pvt_create_hwmon(struct pvt_hwmon *pvt)
1058 pvt->hwmon = devm_hwmon_device_register_with_info(pvt->dev, "pvt", pvt,
1060 if (IS_ERR(pvt->hwmon)) {
1061 dev_err(pvt->dev, "Couldn't create hwmon device\n");
1062 return PTR_ERR(pvt->hwmon);
1072 struct pvt_hwmon *pvt = data;
1074 mutex_lock(&pvt->iface_mtx);
1075 pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
1076 pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID,
1078 mutex_unlock(&pvt->iface_mtx);
1081 static int pvt_enable_iface(struct pvt_hwmon *pvt)
1085 ret = devm_add_action(pvt->dev, pvt_disable_iface, pvt);
1087 dev_err(pvt->dev, "Can't add PVT disable interface action\n");
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);
1107 static int pvt_enable_iface(struct pvt_hwmon *pvt)
1116 struct pvt_hwmon *pvt;
1119 pvt = pvt_create_data(pdev);
1120 if (IS_ERR(pvt))
1121 return PTR_ERR(pvt);
1123 ret = pvt_request_regs(pvt);
1127 ret = pvt_request_clks(pvt);
1131 ret = pvt_check_pwr(pvt);
1135 ret = pvt_init_iface(pvt);
1139 ret = pvt_request_irq(pvt);
1143 ret = pvt_create_hwmon(pvt);
1147 ret = pvt_enable_iface(pvt);
1155 { .compatible = "baikal,bt1-pvt" },
1163 .name = "bt1-pvt",