Linux Audio

Check our new training course

Loading...
v4.6
  1/*
  2 * SPEAr thermal driver.
  3 *
  4 * Copyright (C) 2011-2012 ST Microelectronics
  5 * Author: Vincenzo Frascino <vincenzo.frascino@st.com>
  6 *
  7 * This software is licensed under the terms of the GNU General Public
  8 * License version 2, as published by the Free Software Foundation, and
  9 * may be copied, distributed, and modified under those terms.
 10 *
 11 * This program is distributed in the hope that it will be useful,
 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 14 * GNU General Public License for more details.
 15 *
 16 */
 17
 18#include <linux/clk.h>
 19#include <linux/device.h>
 20#include <linux/err.h>
 21#include <linux/io.h>
 22#include <linux/kernel.h>
 23#include <linux/of.h>
 24#include <linux/module.h>
 25#include <linux/platform_device.h>
 
 26#include <linux/thermal.h>
 27
 28#define MD_FACTOR	1000
 29
 30/* SPEAr Thermal Sensor Dev Structure */
 31struct spear_thermal_dev {
 32	/* pointer to base address of the thermal sensor */
 33	void __iomem *thermal_base;
 34	/* clk structure */
 35	struct clk *clk;
 36	/* pointer to thermal flags */
 37	unsigned int flags;
 38};
 39
 40static inline int thermal_get_temp(struct thermal_zone_device *thermal,
 41				int *temp)
 42{
 43	struct spear_thermal_dev *stdev = thermal->devdata;
 44
 45	/*
 46	 * Data are ready to be read after 628 usec from POWERDOWN signal
 47	 * (PDN) = 1
 48	 */
 49	*temp = (readl_relaxed(stdev->thermal_base) & 0x7F) * MD_FACTOR;
 50	return 0;
 51}
 52
 53static struct thermal_zone_device_ops ops = {
 54	.get_temp = thermal_get_temp,
 55};
 56
 57static int __maybe_unused spear_thermal_suspend(struct device *dev)
 
 58{
 59	struct platform_device *pdev = to_platform_device(dev);
 60	struct thermal_zone_device *spear_thermal = platform_get_drvdata(pdev);
 61	struct spear_thermal_dev *stdev = spear_thermal->devdata;
 62	unsigned int actual_mask = 0;
 63
 64	/* Disable SPEAr Thermal Sensor */
 65	actual_mask = readl_relaxed(stdev->thermal_base);
 66	writel_relaxed(actual_mask & ~stdev->flags, stdev->thermal_base);
 67
 68	clk_disable(stdev->clk);
 69	dev_info(dev, "Suspended.\n");
 70
 71	return 0;
 72}
 73
 74static int __maybe_unused spear_thermal_resume(struct device *dev)
 75{
 76	struct platform_device *pdev = to_platform_device(dev);
 77	struct thermal_zone_device *spear_thermal = platform_get_drvdata(pdev);
 78	struct spear_thermal_dev *stdev = spear_thermal->devdata;
 79	unsigned int actual_mask = 0;
 80	int ret = 0;
 81
 82	ret = clk_enable(stdev->clk);
 83	if (ret) {
 84		dev_err(&pdev->dev, "Can't enable clock\n");
 85		return ret;
 86	}
 87
 88	/* Enable SPEAr Thermal Sensor */
 89	actual_mask = readl_relaxed(stdev->thermal_base);
 90	writel_relaxed(actual_mask | stdev->flags, stdev->thermal_base);
 91
 92	dev_info(dev, "Resumed.\n");
 93
 94	return 0;
 95}
 
 96
 97static SIMPLE_DEV_PM_OPS(spear_thermal_pm_ops, spear_thermal_suspend,
 98		spear_thermal_resume);
 99
100static int spear_thermal_probe(struct platform_device *pdev)
101{
102	struct thermal_zone_device *spear_thermal = NULL;
103	struct spear_thermal_dev *stdev;
104	struct device_node *np = pdev->dev.of_node;
105	struct resource *res;
106	int ret = 0, val;
 
 
 
 
 
107
108	if (!np || !of_property_read_u32(np, "st,thermal-flags", &val)) {
109		dev_err(&pdev->dev, "Failed: DT Pdata not passed\n");
 
110		return -EINVAL;
111	}
112
113	stdev = devm_kzalloc(&pdev->dev, sizeof(*stdev), GFP_KERNEL);
114	if (!stdev)
 
115		return -ENOMEM;
 
116
117	/* Enable thermal sensor */
118	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
119	stdev->thermal_base = devm_ioremap_resource(&pdev->dev, res);
120	if (IS_ERR(stdev->thermal_base))
121		return PTR_ERR(stdev->thermal_base);
 
 
122
123	stdev->clk = devm_clk_get(&pdev->dev, NULL);
124	if (IS_ERR(stdev->clk)) {
125		dev_err(&pdev->dev, "Can't get clock\n");
126		return PTR_ERR(stdev->clk);
127	}
128
129	ret = clk_enable(stdev->clk);
130	if (ret) {
131		dev_err(&pdev->dev, "Can't enable clock\n");
132		return ret;
133	}
134
135	stdev->flags = val;
136	writel_relaxed(stdev->flags, stdev->thermal_base);
137
138	spear_thermal = thermal_zone_device_register("spear_thermal", 0, 0,
139				stdev, &ops, NULL, 0, 0);
140	if (IS_ERR(spear_thermal)) {
141		dev_err(&pdev->dev, "thermal zone device is NULL\n");
142		ret = PTR_ERR(spear_thermal);
143		goto disable_clk;
144	}
145
146	platform_set_drvdata(pdev, spear_thermal);
147
148	dev_info(&spear_thermal->device, "Thermal Sensor Loaded at: 0x%p.\n",
149			stdev->thermal_base);
150
151	return 0;
152
153disable_clk:
154	clk_disable(stdev->clk);
 
 
155
156	return ret;
157}
158
159static int spear_thermal_exit(struct platform_device *pdev)
160{
161	unsigned int actual_mask = 0;
162	struct thermal_zone_device *spear_thermal = platform_get_drvdata(pdev);
163	struct spear_thermal_dev *stdev = spear_thermal->devdata;
164
165	thermal_zone_device_unregister(spear_thermal);
 
166
167	/* Disable SPEAr Thermal Sensor */
168	actual_mask = readl_relaxed(stdev->thermal_base);
169	writel_relaxed(actual_mask & ~stdev->flags, stdev->thermal_base);
170
171	clk_disable(stdev->clk);
 
172
173	return 0;
174}
175
176static const struct of_device_id spear_thermal_id_table[] = {
177	{ .compatible = "st,thermal-spear1340" },
178	{}
179};
180MODULE_DEVICE_TABLE(of, spear_thermal_id_table);
181
182static struct platform_driver spear_thermal_driver = {
183	.probe = spear_thermal_probe,
184	.remove = spear_thermal_exit,
185	.driver = {
186		.name = "spear_thermal",
 
187		.pm = &spear_thermal_pm_ops,
188		.of_match_table = spear_thermal_id_table,
189	},
190};
191
192module_platform_driver(spear_thermal_driver);
193
194MODULE_AUTHOR("Vincenzo Frascino <vincenzo.frascino@st.com>");
195MODULE_DESCRIPTION("SPEAr thermal driver");
196MODULE_LICENSE("GPL");
v3.5.6
  1/*
  2 * SPEAr thermal driver.
  3 *
  4 * Copyright (C) 2011-2012 ST Microelectronics
  5 * Author: Vincenzo Frascino <vincenzo.frascino@st.com>
  6 *
  7 * This software is licensed under the terms of the GNU General Public
  8 * License version 2, as published by the Free Software Foundation, and
  9 * may be copied, distributed, and modified under those terms.
 10 *
 11 * This program is distributed in the hope that it will be useful,
 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 14 * GNU General Public License for more details.
 15 *
 16 */
 17
 18#include <linux/clk.h>
 19#include <linux/device.h>
 20#include <linux/err.h>
 21#include <linux/io.h>
 22#include <linux/kernel.h>
 
 23#include <linux/module.h>
 24#include <linux/platform_device.h>
 25#include <linux/platform_data/spear_thermal.h>
 26#include <linux/thermal.h>
 27
 28#define MD_FACTOR	1000
 29
 30/* SPEAr Thermal Sensor Dev Structure */
 31struct spear_thermal_dev {
 32	/* pointer to base address of the thermal sensor */
 33	void __iomem *thermal_base;
 34	/* clk structure */
 35	struct clk *clk;
 36	/* pointer to thermal flags */
 37	unsigned int flags;
 38};
 39
 40static inline int thermal_get_temp(struct thermal_zone_device *thermal,
 41				unsigned long *temp)
 42{
 43	struct spear_thermal_dev *stdev = thermal->devdata;
 44
 45	/*
 46	 * Data are ready to be read after 628 usec from POWERDOWN signal
 47	 * (PDN) = 1
 48	 */
 49	*temp = (readl_relaxed(stdev->thermal_base) & 0x7F) * MD_FACTOR;
 50	return 0;
 51}
 52
 53static struct thermal_zone_device_ops ops = {
 54	.get_temp = thermal_get_temp,
 55};
 56
 57#ifdef CONFIG_PM
 58static int spear_thermal_suspend(struct device *dev)
 59{
 60	struct platform_device *pdev = to_platform_device(dev);
 61	struct thermal_zone_device *spear_thermal = platform_get_drvdata(pdev);
 62	struct spear_thermal_dev *stdev = spear_thermal->devdata;
 63	unsigned int actual_mask = 0;
 64
 65	/* Disable SPEAr Thermal Sensor */
 66	actual_mask = readl_relaxed(stdev->thermal_base);
 67	writel_relaxed(actual_mask & ~stdev->flags, stdev->thermal_base);
 68
 69	clk_disable(stdev->clk);
 70	dev_info(dev, "Suspended.\n");
 71
 72	return 0;
 73}
 74
 75static int spear_thermal_resume(struct device *dev)
 76{
 77	struct platform_device *pdev = to_platform_device(dev);
 78	struct thermal_zone_device *spear_thermal = platform_get_drvdata(pdev);
 79	struct spear_thermal_dev *stdev = spear_thermal->devdata;
 80	unsigned int actual_mask = 0;
 81	int ret = 0;
 82
 83	ret = clk_enable(stdev->clk);
 84	if (ret) {
 85		dev_err(&pdev->dev, "Can't enable clock\n");
 86		return ret;
 87	}
 88
 89	/* Enable SPEAr Thermal Sensor */
 90	actual_mask = readl_relaxed(stdev->thermal_base);
 91	writel_relaxed(actual_mask | stdev->flags, stdev->thermal_base);
 92
 93	dev_info(dev, "Resumed.\n");
 94
 95	return 0;
 96}
 97#endif
 98
 99static SIMPLE_DEV_PM_OPS(spear_thermal_pm_ops, spear_thermal_suspend,
100		spear_thermal_resume);
101
102static int spear_thermal_probe(struct platform_device *pdev)
103{
104	struct thermal_zone_device *spear_thermal = NULL;
105	struct spear_thermal_dev *stdev;
106	struct spear_thermal_pdata *pdata;
107	int ret = 0;
108	struct resource *stres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
109
110	if (!stres) {
111		dev_err(&pdev->dev, "memory resource missing\n");
112		return -ENODEV;
113	}
114
115	pdata = dev_get_platdata(&pdev->dev);
116	if (!pdata) {
117		dev_err(&pdev->dev, "platform data is NULL\n");
118		return -EINVAL;
119	}
120
121	stdev = devm_kzalloc(&pdev->dev, sizeof(*stdev), GFP_KERNEL);
122	if (!stdev) {
123		dev_err(&pdev->dev, "kzalloc fail\n");
124		return -ENOMEM;
125	}
126
127	/* Enable thermal sensor */
128	stdev->thermal_base = devm_ioremap(&pdev->dev, stres->start,
129			resource_size(stres));
130	if (!stdev->thermal_base) {
131		dev_err(&pdev->dev, "ioremap failed\n");
132		return -ENOMEM;
133	}
134
135	stdev->clk = clk_get(&pdev->dev, NULL);
136	if (IS_ERR(stdev->clk)) {
137		dev_err(&pdev->dev, "Can't get clock\n");
138		return PTR_ERR(stdev->clk);
139	}
140
141	ret = clk_enable(stdev->clk);
142	if (ret) {
143		dev_err(&pdev->dev, "Can't enable clock\n");
144		goto put_clk;
145	}
146
147	stdev->flags = pdata->thermal_flags;
148	writel_relaxed(stdev->flags, stdev->thermal_base);
149
150	spear_thermal = thermal_zone_device_register("spear_thermal", 0,
151				stdev, &ops, 0, 0, 0, 0);
152	if (IS_ERR(spear_thermal)) {
153		dev_err(&pdev->dev, "thermal zone device is NULL\n");
154		ret = PTR_ERR(spear_thermal);
155		goto disable_clk;
156	}
157
158	platform_set_drvdata(pdev, spear_thermal);
159
160	dev_info(&spear_thermal->device, "Thermal Sensor Loaded at: 0x%p.\n",
161			stdev->thermal_base);
162
163	return 0;
164
165disable_clk:
166	clk_disable(stdev->clk);
167put_clk:
168	clk_put(stdev->clk);
169
170	return ret;
171}
172
173static int spear_thermal_exit(struct platform_device *pdev)
174{
175	unsigned int actual_mask = 0;
176	struct thermal_zone_device *spear_thermal = platform_get_drvdata(pdev);
177	struct spear_thermal_dev *stdev = spear_thermal->devdata;
178
179	thermal_zone_device_unregister(spear_thermal);
180	platform_set_drvdata(pdev, NULL);
181
182	/* Disable SPEAr Thermal Sensor */
183	actual_mask = readl_relaxed(stdev->thermal_base);
184	writel_relaxed(actual_mask & ~stdev->flags, stdev->thermal_base);
185
186	clk_disable(stdev->clk);
187	clk_put(stdev->clk);
188
189	return 0;
190}
191
 
 
 
 
 
 
192static struct platform_driver spear_thermal_driver = {
193	.probe = spear_thermal_probe,
194	.remove = spear_thermal_exit,
195	.driver = {
196		.name = "spear_thermal",
197		.owner = THIS_MODULE,
198		.pm = &spear_thermal_pm_ops,
 
199	},
200};
201
202module_platform_driver(spear_thermal_driver);
203
204MODULE_AUTHOR("Vincenzo Frascino <vincenzo.frascino@st.com>");
205MODULE_DESCRIPTION("SPEAr thermal driver");
206MODULE_LICENSE("GPL");