Lines Matching refs:pvt

33 #include "bt1-pvt.h"
158 static inline void pvt_set_mode(struct pvt_hwmon *pvt, u32 mode)
164 old = pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
165 pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_MODE_MASK | PVT_CTRL_EN,
176 static inline void pvt_set_trim(struct pvt_hwmon *pvt, u32 trim)
182 old = pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
183 pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_TRIM_MASK | PVT_CTRL_EN,
187 static inline void pvt_set_tout(struct pvt_hwmon *pvt, u32 tout)
191 old = pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
192 writel(tout, pvt->regs + PVT_TTIMEOUT);
193 pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, old);
231 struct pvt_hwmon *pvt = data;
240 thres_sts = readl(pvt->regs + PVT_RAW_INTR_STAT);
247 cache = &pvt->cache[pvt->sensor];
248 info = &pvt_info[pvt->sensor];
249 pvt->sensor = (pvt->sensor == PVT_SENSOR_LAST) ?
250 PVT_SENSOR_FIRST : (pvt->sensor + 1);
261 mutex_lock(&pvt->iface_mtx);
263 old = pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID,
266 val = readl(pvt->regs + PVT_DATA);
268 pvt_set_mode(pvt, pvt_info[pvt->sensor].mode);
270 pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID, old);
272 mutex_unlock(&pvt->iface_mtx);
293 hwmon_notify_event(pvt->hwmon, info->type, info->attr_min_alarm,
297 hwmon_notify_event(pvt->hwmon, info->type, info->attr_max_alarm,
314 static int pvt_read_data(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
317 struct pvt_cache *cache = &pvt->cache[type];
334 static int pvt_read_limit(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
340 data = readl(pvt->regs + pvt_info[type].thres_base);
355 static int pvt_write_limit(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
370 ret = mutex_lock_interruptible(&pvt->iface_mtx);
375 limit = readl(pvt->regs + pvt_info[type].thres_base);
388 pvt_update(pvt->regs + pvt_info[type].thres_base, mask, data);
390 mutex_unlock(&pvt->iface_mtx);
395 static int pvt_read_alarm(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
399 *val = !!READ_ONCE(pvt->cache[type].thres_sts_lo);
401 *val = !!READ_ONCE(pvt->cache[type].thres_sts_hi);
434 struct pvt_hwmon *pvt = data;
442 pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID,
449 val = readl(pvt->regs + PVT_DATA);
451 dev_err(pvt->dev, "Got IRQ when data isn't valid\n");
455 cache = &pvt->cache[pvt->sensor];
476 static int pvt_read_data(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
479 struct pvt_cache *cache = &pvt->cache[type];
490 ret = mutex_lock_interruptible(&pvt->iface_mtx);
494 pvt->sensor = type;
495 pvt_set_mode(pvt, pvt_info[type].mode);
501 pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID, 0);
502 pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, PVT_CTRL_EN);
510 timeout = 2 * usecs_to_jiffies(ktime_to_us(pvt->timeout));
513 pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
514 pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID,
519 mutex_unlock(&pvt->iface_mtx);
532 static int pvt_read_limit(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
538 static int pvt_write_limit(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
544 static int pvt_read_alarm(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
636 static int pvt_read_trim(struct pvt_hwmon *pvt, long *val)
640 data = readl(pvt->regs + PVT_CTRL);
646 static int pvt_write_trim(struct pvt_hwmon *pvt, long val)
655 ret = mutex_lock_interruptible(&pvt->iface_mtx);
660 pvt_set_trim(pvt, trim);
662 mutex_unlock(&pvt->iface_mtx);
667 static int pvt_read_timeout(struct pvt_hwmon *pvt, long *val)
671 ret = mutex_lock_interruptible(&pvt->iface_mtx);
676 *val = ktime_to_ms(pvt->timeout);
678 mutex_unlock(&pvt->iface_mtx);
683 static int pvt_write_timeout(struct pvt_hwmon *pvt, long val)
690 rate = clk_get_rate(pvt->clks[PVT_CLOCK_REF].clk);
723 ret = mutex_lock_interruptible(&pvt->iface_mtx);
727 pvt_set_tout(pvt, data);
728 pvt->timeout = cache;
730 mutex_unlock(&pvt->iface_mtx);
738 struct pvt_hwmon *pvt = dev_get_drvdata(dev);
747 return pvt_read_timeout(pvt, val);
753 return pvt_read_data(pvt, ch, val);
758 return pvt_read_limit(pvt, ch, true, val);
760 return pvt_read_limit(pvt, ch, false, val);
762 return pvt_read_alarm(pvt, ch, true, val);
764 return pvt_read_alarm(pvt, ch, false, val);
766 return pvt_read_trim(pvt, val);
772 return pvt_read_data(pvt, PVT_VOLT + ch, val);
774 return pvt_read_limit(pvt, PVT_VOLT + ch, true, val);
776 return pvt_read_limit(pvt, PVT_VOLT + ch, false, val);
778 return pvt_read_alarm(pvt, PVT_VOLT + ch, true, val);
780 return pvt_read_alarm(pvt, PVT_VOLT + ch, false, val);
822 struct pvt_hwmon *pvt = dev_get_drvdata(dev);
831 return pvt_write_timeout(pvt, val);
837 return pvt_write_limit(pvt, ch, true, val);
839 return pvt_write_limit(pvt, ch, false, val);
841 return pvt_write_trim(pvt, val);
847 return pvt_write_limit(pvt, PVT_VOLT + ch, true, val);
849 return pvt_write_limit(pvt, PVT_VOLT + ch, false, val);
873 struct pvt_hwmon *pvt = data;
878 complete_all(&pvt->cache[idx].conversion);
881 mutex_destroy(&pvt->iface_mtx);
887 struct pvt_hwmon *pvt;
890 pvt = devm_kzalloc(dev, sizeof(*pvt), GFP_KERNEL);
891 if (!pvt)
894 ret = devm_add_action(dev, pvt_clear_data, pvt);
900 pvt->dev = dev;
901 pvt->sensor = PVT_SENSOR_FIRST;
902 mutex_init(&pvt->iface_mtx);
906 seqlock_init(&pvt->cache[idx].data_seqlock);
909 init_completion(&pvt->cache[idx].conversion);
912 return pvt;
915 static int pvt_request_regs(struct pvt_hwmon *pvt)
917 struct platform_device *pdev = to_platform_device(pvt->dev);
922 dev_err(pvt->dev, "Couldn't find PVT memresource\n");
926 pvt->regs = devm_ioremap_resource(pvt->dev, res);
927 if (IS_ERR(pvt->regs)) {
928 dev_err(pvt->dev, "Couldn't map PVT registers\n");
929 return PTR_ERR(pvt->regs);
937 struct pvt_hwmon *pvt = data;
939 clk_bulk_disable_unprepare(PVT_CLOCK_NUM, pvt->clks);
942 static int pvt_request_clks(struct pvt_hwmon *pvt)
946 pvt->clks[PVT_CLOCK_APB].id = "pclk";
947 pvt->clks[PVT_CLOCK_REF].id = "ref";
949 ret = devm_clk_bulk_get(pvt->dev, PVT_CLOCK_NUM, pvt->clks);
951 dev_err(pvt->dev, "Couldn't get PVT clocks descriptors\n");
955 ret = clk_bulk_prepare_enable(PVT_CLOCK_NUM, pvt->clks);
957 dev_err(pvt->dev, "Couldn't enable the PVT clocks\n");
961 ret = devm_add_action_or_reset(pvt->dev, pvt_disable_clks, pvt);
963 dev_err(pvt->dev, "Can't add PVT clocks disable action\n");
970 static int pvt_check_pwr(struct pvt_hwmon *pvt)
986 pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_ALL, PVT_INTR_ALL);
987 pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, PVT_CTRL_EN);
988 pvt_set_tout(pvt, 0);
989 readl(pvt->regs + PVT_DATA);
994 data = readl(pvt->regs + PVT_DATA);
997 dev_err(pvt->dev, "Sensor is powered down\n");
1000 pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
1005 static int pvt_init_iface(struct pvt_hwmon *pvt)
1010 rate = clk_get_rate(pvt->clks[PVT_CLOCK_REF].clk);
1012 dev_err(pvt->dev, "Invalid reference clock rate\n");
1021 pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_ALL, PVT_INTR_ALL);
1022 pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
1023 readl(pvt->regs + PVT_CLR_INTR);
1024 readl(pvt->regs + PVT_DATA);
1027 pvt_set_mode(pvt, pvt_info[pvt->sensor].mode);
1028 pvt_set_tout(pvt, PVT_TOUT_DEF);
1044 pvt->timeout = ktime_set(PVT_SENSORS_NUM * PVT_TOUT_DEF, 0);
1045 pvt->timeout = ktime_divns(pvt->timeout, rate);
1046 pvt->timeout = ktime_add_ns(pvt->timeout, PVT_SENSORS_NUM * PVT_TOUT_MIN);
1048 pvt->timeout = ktime_set(PVT_TOUT_DEF, 0);
1049 pvt->timeout = ktime_divns(pvt->timeout, rate);
1050 pvt->timeout = ktime_add_ns(pvt->timeout, PVT_TOUT_MIN);
1054 if (!of_property_read_u32(pvt->dev->of_node,
1055 "baikal,pvt-temp-offset-millicelsius", &temp))
1058 pvt_set_trim(pvt, trim);
1063 static int pvt_request_irq(struct pvt_hwmon *pvt)
1065 struct platform_device *pdev = to_platform_device(pvt->dev);
1068 pvt->irq = platform_get_irq(pdev, 0);
1069 if (pvt->irq < 0)
1070 return pvt->irq;
1072 ret = devm_request_threaded_irq(pvt->dev, pvt->irq,
1080 "pvt", pvt);
1082 dev_err(pvt->dev, "Couldn't request PVT IRQ\n");
1089 static int pvt_create_hwmon(struct pvt_hwmon *pvt)
1091 pvt->hwmon = devm_hwmon_device_register_with_info(pvt->dev, "pvt", pvt,
1093 if (IS_ERR(pvt->hwmon)) {
1094 dev_err(pvt->dev, "Couldn't create hwmon device\n");
1095 return PTR_ERR(pvt->hwmon);
1105 struct pvt_hwmon *pvt = data;
1107 mutex_lock(&pvt->iface_mtx);
1108 pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, 0);
1109 pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID,
1111 mutex_unlock(&pvt->iface_mtx);
1114 static int pvt_enable_iface(struct pvt_hwmon *pvt)
1118 ret = devm_add_action(pvt->dev, pvt_disable_iface, pvt);
1120 dev_err(pvt->dev, "Can't add PVT disable interface action\n");
1130 mutex_lock(&pvt->iface_mtx);
1131 pvt_update(pvt->regs + PVT_INTR_MASK, PVT_INTR_DVALID, 0);
1132 pvt_update(pvt->regs + PVT_CTRL, PVT_CTRL_EN, PVT_CTRL_EN);
1133 mutex_unlock(&pvt->iface_mtx);
1140 static int pvt_enable_iface(struct pvt_hwmon *pvt)
1149 struct pvt_hwmon *pvt;
1152 pvt = pvt_create_data(pdev);
1153 if (IS_ERR(pvt))
1154 return PTR_ERR(pvt);
1156 ret = pvt_request_regs(pvt);
1160 ret = pvt_request_clks(pvt);
1164 ret = pvt_check_pwr(pvt);
1168 ret = pvt_init_iface(pvt);
1172 ret = pvt_request_irq(pvt);
1176 ret = pvt_create_hwmon(pvt);
1180 ret = pvt_enable_iface(pvt);
1188 { .compatible = "baikal,bt1-pvt" },
1196 .name = "bt1-pvt",