Linux Audio

Check our new training course

Loading...
Note: File does not exist in v3.1.
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Dove thermal sensor driver
  4 *
  5 * Copyright (C) 2013 Andrew Lunn <andrew@lunn.ch>
  6 */
  7#include <linux/device.h>
  8#include <linux/err.h>
  9#include <linux/io.h>
 10#include <linux/kernel.h>
 11#include <linux/of.h>
 12#include <linux/module.h>
 13#include <linux/platform_device.h>
 14#include <linux/thermal.h>
 15
 16#define DOVE_THERMAL_TEMP_OFFSET	1
 17#define DOVE_THERMAL_TEMP_MASK		0x1FF
 18
 19/* Dove Thermal Manager Control and Status Register */
 20#define PMU_TM_DISABLE_OFFS		0
 21#define PMU_TM_DISABLE_MASK		(0x1 << PMU_TM_DISABLE_OFFS)
 22
 23/* Dove Theraml Diode Control 0 Register */
 24#define PMU_TDC0_SW_RST_MASK		(0x1 << 1)
 25#define PMU_TDC0_SEL_VCAL_OFFS		5
 26#define PMU_TDC0_SEL_VCAL_MASK		(0x3 << PMU_TDC0_SEL_VCAL_OFFS)
 27#define PMU_TDC0_REF_CAL_CNT_OFFS	11
 28#define PMU_TDC0_REF_CAL_CNT_MASK	(0x1FF << PMU_TDC0_REF_CAL_CNT_OFFS)
 29#define PMU_TDC0_AVG_NUM_OFFS		25
 30#define PMU_TDC0_AVG_NUM_MASK		(0x7 << PMU_TDC0_AVG_NUM_OFFS)
 31
 32/* Dove Thermal Diode Control 1 Register */
 33#define PMU_TEMP_DIOD_CTRL1_REG		0x04
 34#define PMU_TDC1_TEMP_VALID_MASK	(0x1 << 10)
 35
 36/* Dove Thermal Sensor Dev Structure */
 37struct dove_thermal_priv {
 38	void __iomem *sensor;
 39	void __iomem *control;
 40};
 41
 42static int dove_init_sensor(const struct dove_thermal_priv *priv)
 43{
 44	u32 reg;
 45	u32 i;
 46
 47	/* Configure the Diode Control Register #0 */
 48	reg = readl_relaxed(priv->control);
 49
 50	/* Use average of 2 */
 51	reg &= ~PMU_TDC0_AVG_NUM_MASK;
 52	reg |= (0x1 << PMU_TDC0_AVG_NUM_OFFS);
 53
 54	/* Reference calibration value */
 55	reg &= ~PMU_TDC0_REF_CAL_CNT_MASK;
 56	reg |= (0x0F1 << PMU_TDC0_REF_CAL_CNT_OFFS);
 57
 58	/* Set the high level reference for calibration */
 59	reg &= ~PMU_TDC0_SEL_VCAL_MASK;
 60	reg |= (0x2 << PMU_TDC0_SEL_VCAL_OFFS);
 61	writel(reg, priv->control);
 62
 63	/* Reset the sensor */
 64	reg = readl_relaxed(priv->control);
 65	writel((reg | PMU_TDC0_SW_RST_MASK), priv->control);
 66	writel(reg, priv->control);
 67
 68	/* Enable the sensor */
 69	reg = readl_relaxed(priv->sensor);
 70	reg &= ~PMU_TM_DISABLE_MASK;
 71	writel(reg, priv->sensor);
 72
 73	/* Poll the sensor for the first reading */
 74	for (i = 0; i < 1000000; i++) {
 75		reg = readl_relaxed(priv->sensor);
 76		if (reg & DOVE_THERMAL_TEMP_MASK)
 77			break;
 78	}
 79
 80	if (i == 1000000)
 81		return -EIO;
 82
 83	return 0;
 84}
 85
 86static int dove_get_temp(struct thermal_zone_device *thermal,
 87			  int *temp)
 88{
 89	unsigned long reg;
 90	struct dove_thermal_priv *priv = thermal->devdata;
 91
 92	/* Valid check */
 93	reg = readl_relaxed(priv->control + PMU_TEMP_DIOD_CTRL1_REG);
 94	if ((reg & PMU_TDC1_TEMP_VALID_MASK) == 0x0) {
 95		dev_err(&thermal->device,
 96			"Temperature sensor reading not valid\n");
 97		return -EIO;
 98	}
 99
100	/*
101	 * Calculate temperature. According to Marvell internal
102	 * documentation the formula for this is:
103	 * Celsius = (322-reg)/1.3625
104	 */
105	reg = readl_relaxed(priv->sensor);
106	reg = (reg >> DOVE_THERMAL_TEMP_OFFSET) & DOVE_THERMAL_TEMP_MASK;
107	*temp = ((3220000000UL - (10000000UL * reg)) / 13625);
108
109	return 0;
110}
111
112static struct thermal_zone_device_ops ops = {
113	.get_temp = dove_get_temp,
114};
115
116static const struct of_device_id dove_thermal_id_table[] = {
117	{ .compatible = "marvell,dove-thermal" },
118	{}
119};
120
121static int dove_thermal_probe(struct platform_device *pdev)
122{
123	struct thermal_zone_device *thermal = NULL;
124	struct dove_thermal_priv *priv;
125	struct resource *res;
126	int ret;
127
128	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
129	if (!priv)
130		return -ENOMEM;
131
132	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
133	priv->sensor = devm_ioremap_resource(&pdev->dev, res);
134	if (IS_ERR(priv->sensor))
135		return PTR_ERR(priv->sensor);
136
137	res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
138	priv->control = devm_ioremap_resource(&pdev->dev, res);
139	if (IS_ERR(priv->control))
140		return PTR_ERR(priv->control);
141
142	ret = dove_init_sensor(priv);
143	if (ret) {
144		dev_err(&pdev->dev, "Failed to initialize sensor\n");
145		return ret;
146	}
147
148	thermal = thermal_zone_device_register("dove_thermal", 0, 0,
149					       priv, &ops, NULL, 0, 0);
150	if (IS_ERR(thermal)) {
151		dev_err(&pdev->dev,
152			"Failed to register thermal zone device\n");
153		return PTR_ERR(thermal);
154	}
155
156	ret = thermal_zone_device_enable(thermal);
157	if (ret) {
158		thermal_zone_device_unregister(thermal);
159		return ret;
160	}
161
162	platform_set_drvdata(pdev, thermal);
163
164	return 0;
165}
166
167static int dove_thermal_exit(struct platform_device *pdev)
168{
169	struct thermal_zone_device *dove_thermal =
170		platform_get_drvdata(pdev);
171
172	thermal_zone_device_unregister(dove_thermal);
173
174	return 0;
175}
176
177MODULE_DEVICE_TABLE(of, dove_thermal_id_table);
178
179static struct platform_driver dove_thermal_driver = {
180	.probe = dove_thermal_probe,
181	.remove = dove_thermal_exit,
182	.driver = {
183		.name = "dove_thermal",
184		.of_match_table = dove_thermal_id_table,
185	},
186};
187
188module_platform_driver(dove_thermal_driver);
189
190MODULE_AUTHOR("Andrew Lunn <andrew@lunn.ch>");
191MODULE_DESCRIPTION("Dove thermal driver");
192MODULE_LICENSE("GPL");