Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * ST Thermal Sensor Driver core routines
0004  * Author: Ajit Pal Singh <ajitpal.singh@st.com>
0005  *
0006  * Copyright (C) 2003-2014 STMicroelectronics (R&D) Limited
0007  */
0008 
0009 #include <linux/clk.h>
0010 #include <linux/module.h>
0011 #include <linux/of.h>
0012 #include <linux/of_device.h>
0013 
0014 #include "st_thermal.h"
0015 
0016 /* The Thermal Framework expects millidegrees */
0017 #define mcelsius(temp)          ((temp) * 1000)
0018 
0019 /*
0020  * Function to allocate regfields which are common
0021  * between syscfg and memory mapped based sensors
0022  */
0023 static int st_thermal_alloc_regfields(struct st_thermal_sensor *sensor)
0024 {
0025     struct device *dev = sensor->dev;
0026     struct regmap *regmap = sensor->regmap;
0027     const struct reg_field *reg_fields = sensor->cdata->reg_fields;
0028 
0029     sensor->dcorrect = devm_regmap_field_alloc(dev, regmap,
0030                            reg_fields[DCORRECT]);
0031 
0032     sensor->overflow = devm_regmap_field_alloc(dev, regmap,
0033                            reg_fields[OVERFLOW]);
0034 
0035     sensor->temp_data = devm_regmap_field_alloc(dev, regmap,
0036                             reg_fields[DATA]);
0037 
0038     if (IS_ERR(sensor->dcorrect) ||
0039         IS_ERR(sensor->overflow) ||
0040         IS_ERR(sensor->temp_data)) {
0041         dev_err(dev, "failed to allocate common regfields\n");
0042         return -EINVAL;
0043     }
0044 
0045     return sensor->ops->alloc_regfields(sensor);
0046 }
0047 
0048 static int st_thermal_sensor_on(struct st_thermal_sensor *sensor)
0049 {
0050     int ret;
0051     struct device *dev = sensor->dev;
0052 
0053     ret = clk_prepare_enable(sensor->clk);
0054     if (ret) {
0055         dev_err(dev, "failed to enable clk\n");
0056         return ret;
0057     }
0058 
0059     ret = sensor->ops->power_ctrl(sensor, POWER_ON);
0060     if (ret) {
0061         dev_err(dev, "failed to power on sensor\n");
0062         clk_disable_unprepare(sensor->clk);
0063     }
0064 
0065     return ret;
0066 }
0067 
0068 static int st_thermal_sensor_off(struct st_thermal_sensor *sensor)
0069 {
0070     int ret;
0071 
0072     ret = sensor->ops->power_ctrl(sensor, POWER_OFF);
0073     if (ret)
0074         return ret;
0075 
0076     clk_disable_unprepare(sensor->clk);
0077 
0078     return 0;
0079 }
0080 
0081 static int st_thermal_calibration(struct st_thermal_sensor *sensor)
0082 {
0083     int ret;
0084     unsigned int val;
0085     struct device *dev = sensor->dev;
0086 
0087     /* Check if sensor calibration data is already written */
0088     ret = regmap_field_read(sensor->dcorrect, &val);
0089     if (ret) {
0090         dev_err(dev, "failed to read calibration data\n");
0091         return ret;
0092     }
0093 
0094     if (!val) {
0095         /*
0096          * Sensor calibration value not set by bootloader,
0097          * default calibration data to be used
0098          */
0099         ret = regmap_field_write(sensor->dcorrect,
0100                      sensor->cdata->calibration_val);
0101         if (ret)
0102             dev_err(dev, "failed to set calibration data\n");
0103     }
0104 
0105     return ret;
0106 }
0107 
0108 /* Callback to get temperature from HW*/
0109 static int st_thermal_get_temp(struct thermal_zone_device *th, int *temperature)
0110 {
0111     struct st_thermal_sensor *sensor = th->devdata;
0112     struct device *dev = sensor->dev;
0113     unsigned int temp;
0114     unsigned int overflow;
0115     int ret;
0116 
0117     ret = regmap_field_read(sensor->overflow, &overflow);
0118     if (ret)
0119         return ret;
0120     if (overflow)
0121         return -EIO;
0122 
0123     ret = regmap_field_read(sensor->temp_data, &temp);
0124     if (ret)
0125         return ret;
0126 
0127     temp += sensor->cdata->temp_adjust_val;
0128     temp = mcelsius(temp);
0129 
0130     dev_dbg(dev, "temperature: %d\n", temp);
0131 
0132     *temperature = temp;
0133 
0134     return 0;
0135 }
0136 
0137 static int st_thermal_get_trip_type(struct thermal_zone_device *th,
0138                 int trip, enum thermal_trip_type *type)
0139 {
0140     struct st_thermal_sensor *sensor = th->devdata;
0141     struct device *dev = sensor->dev;
0142 
0143     switch (trip) {
0144     case 0:
0145         *type = THERMAL_TRIP_CRITICAL;
0146         break;
0147     default:
0148         dev_err(dev, "invalid trip point\n");
0149         return -EINVAL;
0150     }
0151 
0152     return 0;
0153 }
0154 
0155 static int st_thermal_get_trip_temp(struct thermal_zone_device *th,
0156                     int trip, int *temp)
0157 {
0158     struct st_thermal_sensor *sensor = th->devdata;
0159     struct device *dev = sensor->dev;
0160 
0161     switch (trip) {
0162     case 0:
0163         *temp = mcelsius(sensor->cdata->crit_temp);
0164         break;
0165     default:
0166         dev_err(dev, "Invalid trip point\n");
0167         return -EINVAL;
0168     }
0169 
0170     return 0;
0171 }
0172 
0173 static struct thermal_zone_device_ops st_tz_ops = {
0174     .get_temp   = st_thermal_get_temp,
0175     .get_trip_type  = st_thermal_get_trip_type,
0176     .get_trip_temp  = st_thermal_get_trip_temp,
0177 };
0178 
0179 int st_thermal_register(struct platform_device *pdev,
0180             const struct of_device_id *st_thermal_of_match)
0181 {
0182     struct st_thermal_sensor *sensor;
0183     struct device *dev = &pdev->dev;
0184     struct device_node *np = dev->of_node;
0185     const struct of_device_id *match;
0186 
0187     int polling_delay;
0188     int ret;
0189 
0190     if (!np) {
0191         dev_err(dev, "device tree node not found\n");
0192         return -EINVAL;
0193     }
0194 
0195     sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
0196     if (!sensor)
0197         return -ENOMEM;
0198 
0199     sensor->dev = dev;
0200 
0201     match = of_match_device(st_thermal_of_match, dev);
0202     if (!(match && match->data))
0203         return -EINVAL;
0204 
0205     sensor->cdata = match->data;
0206     if (!sensor->cdata->ops)
0207         return -EINVAL;
0208 
0209     sensor->ops = sensor->cdata->ops;
0210 
0211     ret = (sensor->ops->regmap_init)(sensor);
0212     if (ret)
0213         return ret;
0214 
0215     ret = st_thermal_alloc_regfields(sensor);
0216     if (ret)
0217         return ret;
0218 
0219     sensor->clk = devm_clk_get(dev, "thermal");
0220     if (IS_ERR(sensor->clk)) {
0221         dev_err(dev, "failed to fetch clock\n");
0222         return PTR_ERR(sensor->clk);
0223     }
0224 
0225     if (sensor->ops->register_enable_irq) {
0226         ret = sensor->ops->register_enable_irq(sensor);
0227         if (ret)
0228             return ret;
0229     }
0230 
0231     ret = st_thermal_sensor_on(sensor);
0232     if (ret)
0233         return ret;
0234 
0235     ret = st_thermal_calibration(sensor);
0236     if (ret)
0237         goto sensor_off;
0238 
0239     polling_delay = sensor->ops->register_enable_irq ? 0 : 1000;
0240 
0241     sensor->thermal_dev =
0242         thermal_zone_device_register(dev_name(dev), 1, 0, sensor,
0243                          &st_tz_ops, NULL, 0, polling_delay);
0244     if (IS_ERR(sensor->thermal_dev)) {
0245         dev_err(dev, "failed to register thermal zone device\n");
0246         ret = PTR_ERR(sensor->thermal_dev);
0247         goto sensor_off;
0248     }
0249     ret = thermal_zone_device_enable(sensor->thermal_dev);
0250     if (ret)
0251         goto tzd_unregister;
0252 
0253     platform_set_drvdata(pdev, sensor);
0254 
0255     return 0;
0256 
0257 tzd_unregister:
0258     thermal_zone_device_unregister(sensor->thermal_dev);
0259 sensor_off:
0260     st_thermal_sensor_off(sensor);
0261 
0262     return ret;
0263 }
0264 EXPORT_SYMBOL_GPL(st_thermal_register);
0265 
0266 int st_thermal_unregister(struct platform_device *pdev)
0267 {
0268     struct st_thermal_sensor *sensor = platform_get_drvdata(pdev);
0269 
0270     st_thermal_sensor_off(sensor);
0271     thermal_zone_device_unregister(sensor->thermal_dev);
0272 
0273     return 0;
0274 }
0275 EXPORT_SYMBOL_GPL(st_thermal_unregister);
0276 
0277 #ifdef CONFIG_PM_SLEEP
0278 static int st_thermal_suspend(struct device *dev)
0279 {
0280     struct st_thermal_sensor *sensor = dev_get_drvdata(dev);
0281 
0282     return st_thermal_sensor_off(sensor);
0283 }
0284 
0285 static int st_thermal_resume(struct device *dev)
0286 {
0287     int ret;
0288     struct st_thermal_sensor *sensor = dev_get_drvdata(dev);
0289 
0290     ret = st_thermal_sensor_on(sensor);
0291     if (ret)
0292         return ret;
0293 
0294     ret = st_thermal_calibration(sensor);
0295     if (ret)
0296         return ret;
0297 
0298     if (sensor->ops->enable_irq) {
0299         ret = sensor->ops->enable_irq(sensor);
0300         if (ret)
0301             return ret;
0302     }
0303 
0304     return 0;
0305 }
0306 #endif
0307 
0308 SIMPLE_DEV_PM_OPS(st_thermal_pm_ops, st_thermal_suspend, st_thermal_resume);
0309 EXPORT_SYMBOL_GPL(st_thermal_pm_ops);
0310 
0311 MODULE_AUTHOR("STMicroelectronics (R&D) Limited <ajitpal.singh@st.com>");
0312 MODULE_DESCRIPTION("STMicroelectronics STi SoC Thermal Sensor Driver");
0313 MODULE_LICENSE("GPL v2");