Vincenzo Frascino | 6a92c36 | 2012-03-21 12:55:03 -0700 | [diff] [blame] | 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> |
Viresh Kumar | b9c7aff | 2012-05-29 11:18:51 -0700 | [diff] [blame] | 23 | #include <linux/of.h> |
Vincenzo Frascino | 6a92c36 | 2012-03-21 12:55:03 -0700 | [diff] [blame] | 24 | #include <linux/module.h> |
| 25 | #include <linux/platform_device.h> |
Vincenzo Frascino | 6a92c36 | 2012-03-21 12:55:03 -0700 | [diff] [blame] | 26 | #include <linux/thermal.h> |
| 27 | |
| 28 | #define MD_FACTOR 1000 |
| 29 | |
| 30 | /* SPEAr Thermal Sensor Dev Structure */ |
| 31 | struct 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 | |
| 40 | static 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 | */ |
Viresh Kumar | de716e3 | 2012-03-21 12:55:03 -0700 | [diff] [blame] | 49 | *temp = (readl_relaxed(stdev->thermal_base) & 0x7F) * MD_FACTOR; |
Vincenzo Frascino | 6a92c36 | 2012-03-21 12:55:03 -0700 | [diff] [blame] | 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | static struct thermal_zone_device_ops ops = { |
| 54 | .get_temp = thermal_get_temp, |
| 55 | }; |
| 56 | |
| 57 | #ifdef CONFIG_PM |
| 58 | static 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 */ |
Viresh Kumar | de716e3 | 2012-03-21 12:55:03 -0700 | [diff] [blame] | 66 | actual_mask = readl_relaxed(stdev->thermal_base); |
| 67 | writel_relaxed(actual_mask & ~stdev->flags, stdev->thermal_base); |
Vincenzo Frascino | 6a92c36 | 2012-03-21 12:55:03 -0700 | [diff] [blame] | 68 | |
| 69 | clk_disable(stdev->clk); |
| 70 | dev_info(dev, "Suspended.\n"); |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | static 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 */ |
Viresh Kumar | de716e3 | 2012-03-21 12:55:03 -0700 | [diff] [blame] | 90 | actual_mask = readl_relaxed(stdev->thermal_base); |
| 91 | writel_relaxed(actual_mask | stdev->flags, stdev->thermal_base); |
Vincenzo Frascino | 6a92c36 | 2012-03-21 12:55:03 -0700 | [diff] [blame] | 92 | |
| 93 | dev_info(dev, "Resumed.\n"); |
| 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | #endif |
| 98 | |
| 99 | static SIMPLE_DEV_PM_OPS(spear_thermal_pm_ops, spear_thermal_suspend, |
| 100 | spear_thermal_resume); |
| 101 | |
| 102 | static int spear_thermal_probe(struct platform_device *pdev) |
| 103 | { |
| 104 | struct thermal_zone_device *spear_thermal = NULL; |
| 105 | struct spear_thermal_dev *stdev; |
Viresh Kumar | b9c7aff | 2012-05-29 11:18:51 -0700 | [diff] [blame] | 106 | struct device_node *np = pdev->dev.of_node; |
Zhang Rui | 253e3ae | 2013-05-16 02:16:20 +0000 | [diff] [blame] | 107 | struct resource *res; |
Viresh Kumar | b9c7aff | 2012-05-29 11:18:51 -0700 | [diff] [blame] | 108 | int ret = 0, val; |
| 109 | |
| 110 | if (!np || !of_property_read_u32(np, "st,thermal-flags", &val)) { |
| 111 | dev_err(&pdev->dev, "Failed: DT Pdata not passed\n"); |
| 112 | return -EINVAL; |
| 113 | } |
Vincenzo Frascino | 6a92c36 | 2012-03-21 12:55:03 -0700 | [diff] [blame] | 114 | |
Vincenzo Frascino | 6a92c36 | 2012-03-21 12:55:03 -0700 | [diff] [blame] | 115 | stdev = devm_kzalloc(&pdev->dev, sizeof(*stdev), GFP_KERNEL); |
Jingoo Han | fa018d3 | 2014-05-07 15:05:32 +0900 | [diff] [blame] | 116 | if (!stdev) |
Vincenzo Frascino | 6a92c36 | 2012-03-21 12:55:03 -0700 | [diff] [blame] | 117 | return -ENOMEM; |
Vincenzo Frascino | 6a92c36 | 2012-03-21 12:55:03 -0700 | [diff] [blame] | 118 | |
| 119 | /* Enable thermal sensor */ |
Zhang Rui | c21bec8 | 2013-05-16 02:16:21 +0000 | [diff] [blame] | 120 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 121 | stdev->thermal_base = devm_ioremap_resource(&pdev->dev, res); |
| 122 | if (IS_ERR(stdev->thermal_base)) |
Zhang Rui | 253e3ae | 2013-05-16 02:16:20 +0000 | [diff] [blame] | 123 | return PTR_ERR(stdev->thermal_base); |
Vincenzo Frascino | 6a92c36 | 2012-03-21 12:55:03 -0700 | [diff] [blame] | 124 | |
Julia Lawall | 03b79bd | 2012-12-07 11:29:32 +0100 | [diff] [blame] | 125 | stdev->clk = devm_clk_get(&pdev->dev, NULL); |
Vincenzo Frascino | 6a92c36 | 2012-03-21 12:55:03 -0700 | [diff] [blame] | 126 | if (IS_ERR(stdev->clk)) { |
| 127 | dev_err(&pdev->dev, "Can't get clock\n"); |
| 128 | return PTR_ERR(stdev->clk); |
| 129 | } |
| 130 | |
| 131 | ret = clk_enable(stdev->clk); |
| 132 | if (ret) { |
| 133 | dev_err(&pdev->dev, "Can't enable clock\n"); |
Julia Lawall | 03b79bd | 2012-12-07 11:29:32 +0100 | [diff] [blame] | 134 | return ret; |
Vincenzo Frascino | 6a92c36 | 2012-03-21 12:55:03 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Viresh Kumar | b9c7aff | 2012-05-29 11:18:51 -0700 | [diff] [blame] | 137 | stdev->flags = val; |
Viresh Kumar | de716e3 | 2012-03-21 12:55:03 -0700 | [diff] [blame] | 138 | writel_relaxed(stdev->flags, stdev->thermal_base); |
Vincenzo Frascino | 6a92c36 | 2012-03-21 12:55:03 -0700 | [diff] [blame] | 139 | |
Durgadoss R | c56f5c0 | 2012-07-25 10:10:58 +0800 | [diff] [blame] | 140 | spear_thermal = thermal_zone_device_register("spear_thermal", 0, 0, |
Durgadoss R | 50125a9 | 2012-09-18 11:04:56 +0530 | [diff] [blame] | 141 | stdev, &ops, NULL, 0, 0); |
Dan Carpenter | 03ee62f | 2012-03-21 12:55:04 -0700 | [diff] [blame] | 142 | if (IS_ERR(spear_thermal)) { |
Vincenzo Frascino | 6a92c36 | 2012-03-21 12:55:03 -0700 | [diff] [blame] | 143 | dev_err(&pdev->dev, "thermal zone device is NULL\n"); |
Dan Carpenter | 03ee62f | 2012-03-21 12:55:04 -0700 | [diff] [blame] | 144 | ret = PTR_ERR(spear_thermal); |
Vincenzo Frascino | 6a92c36 | 2012-03-21 12:55:03 -0700 | [diff] [blame] | 145 | goto disable_clk; |
| 146 | } |
| 147 | |
| 148 | platform_set_drvdata(pdev, spear_thermal); |
| 149 | |
| 150 | dev_info(&spear_thermal->device, "Thermal Sensor Loaded at: 0x%p.\n", |
| 151 | stdev->thermal_base); |
| 152 | |
| 153 | return 0; |
| 154 | |
| 155 | disable_clk: |
| 156 | clk_disable(stdev->clk); |
Vincenzo Frascino | 6a92c36 | 2012-03-21 12:55:03 -0700 | [diff] [blame] | 157 | |
| 158 | return ret; |
| 159 | } |
| 160 | |
| 161 | static int spear_thermal_exit(struct platform_device *pdev) |
| 162 | { |
| 163 | unsigned int actual_mask = 0; |
| 164 | struct thermal_zone_device *spear_thermal = platform_get_drvdata(pdev); |
| 165 | struct spear_thermal_dev *stdev = spear_thermal->devdata; |
| 166 | |
| 167 | thermal_zone_device_unregister(spear_thermal); |
Vincenzo Frascino | 6a92c36 | 2012-03-21 12:55:03 -0700 | [diff] [blame] | 168 | |
| 169 | /* Disable SPEAr Thermal Sensor */ |
Viresh Kumar | de716e3 | 2012-03-21 12:55:03 -0700 | [diff] [blame] | 170 | actual_mask = readl_relaxed(stdev->thermal_base); |
| 171 | writel_relaxed(actual_mask & ~stdev->flags, stdev->thermal_base); |
Vincenzo Frascino | 6a92c36 | 2012-03-21 12:55:03 -0700 | [diff] [blame] | 172 | |
| 173 | clk_disable(stdev->clk); |
Vincenzo Frascino | 6a92c36 | 2012-03-21 12:55:03 -0700 | [diff] [blame] | 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |
Viresh Kumar | b9c7aff | 2012-05-29 11:18:51 -0700 | [diff] [blame] | 178 | static const struct of_device_id spear_thermal_id_table[] = { |
| 179 | { .compatible = "st,thermal-spear1340" }, |
| 180 | {} |
| 181 | }; |
| 182 | MODULE_DEVICE_TABLE(of, spear_thermal_id_table); |
| 183 | |
Vincenzo Frascino | 6a92c36 | 2012-03-21 12:55:03 -0700 | [diff] [blame] | 184 | static struct platform_driver spear_thermal_driver = { |
| 185 | .probe = spear_thermal_probe, |
| 186 | .remove = spear_thermal_exit, |
| 187 | .driver = { |
| 188 | .name = "spear_thermal", |
Vincenzo Frascino | 6a92c36 | 2012-03-21 12:55:03 -0700 | [diff] [blame] | 189 | .pm = &spear_thermal_pm_ops, |
Sachin Kamat | 5454f21 | 2013-05-16 10:28:11 +0000 | [diff] [blame] | 190 | .of_match_table = spear_thermal_id_table, |
Vincenzo Frascino | 6a92c36 | 2012-03-21 12:55:03 -0700 | [diff] [blame] | 191 | }, |
| 192 | }; |
| 193 | |
| 194 | module_platform_driver(spear_thermal_driver); |
| 195 | |
| 196 | MODULE_AUTHOR("Vincenzo Frascino <vincenzo.frascino@st.com>"); |
| 197 | MODULE_DESCRIPTION("SPEAr thermal driver"); |
| 198 | MODULE_LICENSE("GPL"); |