0001
0002
0003
0004
0005
0006
0007 #include <linux/device.h>
0008 #include <linux/err.h>
0009 #include <linux/io.h>
0010 #include <linux/kernel.h>
0011 #include <linux/of.h>
0012 #include <linux/module.h>
0013 #include <linux/platform_device.h>
0014 #include <linux/thermal.h>
0015
0016 #define DOVE_THERMAL_TEMP_OFFSET 1
0017 #define DOVE_THERMAL_TEMP_MASK 0x1FF
0018
0019
0020 #define PMU_TM_DISABLE_OFFS 0
0021 #define PMU_TM_DISABLE_MASK (0x1 << PMU_TM_DISABLE_OFFS)
0022
0023
0024 #define PMU_TDC0_SW_RST_MASK (0x1 << 1)
0025 #define PMU_TDC0_SEL_VCAL_OFFS 5
0026 #define PMU_TDC0_SEL_VCAL_MASK (0x3 << PMU_TDC0_SEL_VCAL_OFFS)
0027 #define PMU_TDC0_REF_CAL_CNT_OFFS 11
0028 #define PMU_TDC0_REF_CAL_CNT_MASK (0x1FF << PMU_TDC0_REF_CAL_CNT_OFFS)
0029 #define PMU_TDC0_AVG_NUM_OFFS 25
0030 #define PMU_TDC0_AVG_NUM_MASK (0x7 << PMU_TDC0_AVG_NUM_OFFS)
0031
0032
0033 #define PMU_TEMP_DIOD_CTRL1_REG 0x04
0034 #define PMU_TDC1_TEMP_VALID_MASK (0x1 << 10)
0035
0036
0037 struct dove_thermal_priv {
0038 void __iomem *sensor;
0039 void __iomem *control;
0040 };
0041
0042 static int dove_init_sensor(const struct dove_thermal_priv *priv)
0043 {
0044 u32 reg;
0045 u32 i;
0046
0047
0048 reg = readl_relaxed(priv->control);
0049
0050
0051 reg &= ~PMU_TDC0_AVG_NUM_MASK;
0052 reg |= (0x1 << PMU_TDC0_AVG_NUM_OFFS);
0053
0054
0055 reg &= ~PMU_TDC0_REF_CAL_CNT_MASK;
0056 reg |= (0x0F1 << PMU_TDC0_REF_CAL_CNT_OFFS);
0057
0058
0059 reg &= ~PMU_TDC0_SEL_VCAL_MASK;
0060 reg |= (0x2 << PMU_TDC0_SEL_VCAL_OFFS);
0061 writel(reg, priv->control);
0062
0063
0064 reg = readl_relaxed(priv->control);
0065 writel((reg | PMU_TDC0_SW_RST_MASK), priv->control);
0066 writel(reg, priv->control);
0067
0068
0069 reg = readl_relaxed(priv->sensor);
0070 reg &= ~PMU_TM_DISABLE_MASK;
0071 writel(reg, priv->sensor);
0072
0073
0074 for (i = 0; i < 1000000; i++) {
0075 reg = readl_relaxed(priv->sensor);
0076 if (reg & DOVE_THERMAL_TEMP_MASK)
0077 break;
0078 }
0079
0080 if (i == 1000000)
0081 return -EIO;
0082
0083 return 0;
0084 }
0085
0086 static int dove_get_temp(struct thermal_zone_device *thermal,
0087 int *temp)
0088 {
0089 unsigned long reg;
0090 struct dove_thermal_priv *priv = thermal->devdata;
0091
0092
0093 reg = readl_relaxed(priv->control + PMU_TEMP_DIOD_CTRL1_REG);
0094 if ((reg & PMU_TDC1_TEMP_VALID_MASK) == 0x0) {
0095 dev_err(&thermal->device,
0096 "Temperature sensor reading not valid\n");
0097 return -EIO;
0098 }
0099
0100
0101
0102
0103
0104
0105 reg = readl_relaxed(priv->sensor);
0106 reg = (reg >> DOVE_THERMAL_TEMP_OFFSET) & DOVE_THERMAL_TEMP_MASK;
0107 *temp = ((3220000000UL - (10000000UL * reg)) / 13625);
0108
0109 return 0;
0110 }
0111
0112 static struct thermal_zone_device_ops ops = {
0113 .get_temp = dove_get_temp,
0114 };
0115
0116 static const struct of_device_id dove_thermal_id_table[] = {
0117 { .compatible = "marvell,dove-thermal" },
0118 {}
0119 };
0120
0121 static int dove_thermal_probe(struct platform_device *pdev)
0122 {
0123 struct thermal_zone_device *thermal = NULL;
0124 struct dove_thermal_priv *priv;
0125 struct resource *res;
0126 int ret;
0127
0128 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
0129 if (!priv)
0130 return -ENOMEM;
0131
0132 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0133 priv->sensor = devm_ioremap_resource(&pdev->dev, res);
0134 if (IS_ERR(priv->sensor))
0135 return PTR_ERR(priv->sensor);
0136
0137 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
0138 priv->control = devm_ioremap_resource(&pdev->dev, res);
0139 if (IS_ERR(priv->control))
0140 return PTR_ERR(priv->control);
0141
0142 ret = dove_init_sensor(priv);
0143 if (ret) {
0144 dev_err(&pdev->dev, "Failed to initialize sensor\n");
0145 return ret;
0146 }
0147
0148 thermal = thermal_zone_device_register("dove_thermal", 0, 0,
0149 priv, &ops, NULL, 0, 0);
0150 if (IS_ERR(thermal)) {
0151 dev_err(&pdev->dev,
0152 "Failed to register thermal zone device\n");
0153 return PTR_ERR(thermal);
0154 }
0155
0156 ret = thermal_zone_device_enable(thermal);
0157 if (ret) {
0158 thermal_zone_device_unregister(thermal);
0159 return ret;
0160 }
0161
0162 platform_set_drvdata(pdev, thermal);
0163
0164 return 0;
0165 }
0166
0167 static int dove_thermal_exit(struct platform_device *pdev)
0168 {
0169 struct thermal_zone_device *dove_thermal =
0170 platform_get_drvdata(pdev);
0171
0172 thermal_zone_device_unregister(dove_thermal);
0173
0174 return 0;
0175 }
0176
0177 MODULE_DEVICE_TABLE(of, dove_thermal_id_table);
0178
0179 static struct platform_driver dove_thermal_driver = {
0180 .probe = dove_thermal_probe,
0181 .remove = dove_thermal_exit,
0182 .driver = {
0183 .name = "dove_thermal",
0184 .of_match_table = dove_thermal_id_table,
0185 },
0186 };
0187
0188 module_platform_driver(dove_thermal_driver);
0189
0190 MODULE_AUTHOR("Andrew Lunn <andrew@lunn.ch>");
0191 MODULE_DESCRIPTION("Dove thermal driver");
0192 MODULE_LICENSE("GPL");