thermal: Add palmas thermal support
[linux-3.10.git] / drivers / thermal / palmas_thermal.c
1 /*
2  * palmas_thermal.c -- TI PALMAS THERMAL.
3  *
4  * Copyright (c) 2013, NVIDIA Corporation. All rights reserved.
5  *
6  * Author: Pradeep Goudagunta <pgoudagunta@nvidia.com>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation version 2.
11  *
12  * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
13  * whether express or implied; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20  * 02111-1307, USA
21  */
22 #include <linux/module.h>
23 #include <linux/err.h>
24 #include <linux/irq.h>
25 #include <linux/interrupt.h>
26 #include <linux/platform_device.h>
27 #include <linux/slab.h>
28 #include <linux/delay.h>
29 #include <linux/pm.h>
30 #include <linux/thermal.h>
31 #include <linux/mfd/palmas.h>
32
33 #define PALMAS_NORMAL_OPERATING_TEMP 100000
34 #define PALMAS_CRITICAL_DEFUALT_TEMP 108000
35
36 struct palmas_therm_zone {
37         struct device                   *dev;
38         struct palmas                   *palmas;
39         struct thermal_zone_device      *tz_device;
40         int                             irq;
41         int                             is_crit_temp;
42 };
43
44 struct palmas_trip_point {
45         unsigned long temp;
46          enum thermal_trip_type type;
47 };
48
49 static struct palmas_trip_point palmas_tpoint = {
50                 .temp = PALMAS_CRITICAL_DEFUALT_TEMP,
51                 .type = THERMAL_TRIP_CRITICAL,
52 };
53
54 static int palmas_thermal_get_crit_temp(struct thermal_zone_device *tz_device,
55                         unsigned long *temp)
56 {
57         *temp = palmas_tpoint.temp;
58
59         return 0;
60 }
61
62 static int palmas_thermal_get_temp(struct thermal_zone_device *tz_device,
63                         unsigned long *temp)
64 {
65         struct palmas_therm_zone *ptherm_zone = tz_device->devdata;
66
67         if (ptherm_zone->is_crit_temp) {
68                 /*
69                  * Set temperature greater than Critical trip
70                  * temp to trigger orderly power down sequence
71                  */
72                 palmas_thermal_get_crit_temp(tz_device, temp);
73                 *temp += 1;
74                 return 0;
75         }
76
77         *temp = PALMAS_NORMAL_OPERATING_TEMP;
78         return 0;
79 }
80
81 static int palmas_thermal_get_trip_type(struct thermal_zone_device *tz_device,
82                         int trip, enum thermal_trip_type *type)
83 {
84         if (trip >= 1)
85                 return -EINVAL;
86
87         *type = palmas_tpoint.type;
88         return 0;
89 }
90
91 static int palmas_thermal_get_trip_temp(struct thermal_zone_device *tz_device,
92                         int trip, unsigned long *temp)
93 {
94         if (trip >= 1)
95                 return -EINVAL;
96
97         *temp = palmas_tpoint.temp;
98         return 0;
99 }
100
101 static struct thermal_zone_device_ops palmas_tz_ops = {
102         .get_temp = palmas_thermal_get_temp,
103         .get_crit_temp = palmas_thermal_get_crit_temp,
104         .get_trip_type = palmas_thermal_get_trip_type,
105         .get_trip_temp = palmas_thermal_get_trip_temp,
106 };
107
108 static irqreturn_t palmas_thermal_irq(int irq, void *data)
109 {
110         struct palmas_therm_zone *ptherm_zone = data;
111
112         ptherm_zone->is_crit_temp = 1;
113         /*
114          * Necessary action can be taken here
115          * e.g: thermal_zone_device_update(pz->tz_device);
116          * will trigger orderly power down sequence.
117          */
118
119         return IRQ_HANDLED;
120 }
121
122 static int palmas_thermal_probe(struct platform_device *pdev)
123 {
124         struct palmas_therm_zone *ptherm_zone;
125         struct palmas_platform_data *pdata;
126         struct palmas *palmas = dev_get_drvdata(pdev->dev.parent);
127         char *default_tz_name = "palmas-junc-tz";
128         int ret;
129         u8 val;
130
131         pdata = dev_get_platdata(pdev->dev.parent);
132
133         if (!pdata || !(pdata->hd_threshold_temp)) {
134                 dev_err(&pdev->dev, "No platform data\n");
135                 return -ENODEV;
136         }
137
138         ptherm_zone = devm_kzalloc(&pdev->dev, sizeof(*ptherm_zone),
139                         GFP_KERNEL);
140         if (!ptherm_zone) {
141                 dev_err(&pdev->dev, "No available free memory\n");
142                 return -ENOMEM;
143         }
144
145         platform_set_drvdata(pdev, ptherm_zone);
146         ptherm_zone->dev = &pdev->dev;
147         if (!(pdata->tz_name))
148                 pdata->tz_name = default_tz_name;
149
150         ptherm_zone->tz_device = thermal_zone_device_register(pdata->tz_name,
151                                         1, 0, ptherm_zone, &palmas_tz_ops,
152                                         NULL, 0, 0);
153         if (IS_ERR_OR_NULL(ptherm_zone->tz_device)) {
154                 dev_err(ptherm_zone->dev,
155                         "Register thermal zone device failed.\n");
156                 return PTR_ERR(ptherm_zone->tz_device);
157         }
158
159         palmas_tpoint.temp = pdata->hd_threshold_temp;
160
161         ptherm_zone->irq = platform_get_irq(pdev, 0);
162         ret = request_threaded_irq(ptherm_zone->irq, NULL,
163                         palmas_thermal_irq,
164                         IRQF_ONESHOT, dev_name(&pdev->dev),
165                         ptherm_zone);
166         if (ret < 0) {
167                 dev_err(&pdev->dev,
168                         "request irq %d failed: %dn", ptherm_zone->irq, ret);
169                 goto int_req_failed;
170         }
171
172         switch (palmas_tpoint.temp) {
173         case 108000:
174                 val = 0;
175                 break;
176         case 112000:
177                 val = 1;
178                 break;
179         case 116000:
180                 val = 2;
181                 break;
182         case 120000:
183                 val = 3;
184                 break;
185         default:
186                 dev_err(&pdev->dev, "%ld threshold is not supported",
187                                 palmas_tpoint.temp);
188                 ret = -EINVAL;
189                 goto error;
190         }
191
192         val <<= PALMAS_OSC_THERM_CTRL_THERM_HD_SEL_SHIFT;
193         ret = palmas_update_bits(palmas, PALMAS_PMU_CONTROL_BASE,
194                         PALMAS_OSC_THERM_CTRL,
195                         PALMAS_OSC_THERM_CTRL_THERM_HD_SEL_MASK, val);
196         if (ret < 0) {
197                 dev_err(&pdev->dev, "osc_therm_ctrl reg update failed.\n");
198                 goto error;
199         }
200
201         return 0;
202
203 error:
204         free_irq(ptherm_zone->irq, ptherm_zone);
205 int_req_failed:
206         thermal_zone_device_unregister(ptherm_zone->tz_device);
207         return ret;
208 }
209
210 static int palmas_thermal_remove(struct platform_device *pdev)
211 {
212         struct palmas_therm_zone *ptherm_zone = platform_get_drvdata(pdev);
213
214         thermal_zone_device_unregister(ptherm_zone->tz_device);
215         free_irq(ptherm_zone->irq, ptherm_zone);
216         kfree(ptherm_zone);
217         return 0;
218 }
219
220 static struct platform_driver palmas_thermal_driver = {
221         .probe = palmas_thermal_probe,
222         .remove = palmas_thermal_remove,
223         .driver = {
224                 .name = "palmas-thermal",
225                 .owner = THIS_MODULE,
226         },
227 };
228
229 static int __init palmas_thermal_init(void)
230 {
231         return platform_driver_register(&palmas_thermal_driver);
232 }
233 module_init(palmas_thermal_init);
234
235 static void __exit palmas_thermal_exit(void)
236 {
237         platform_driver_unregister(&palmas_thermal_driver);
238 }
239 module_exit(palmas_thermal_exit);
240
241 MODULE_DESCRIPTION("Palmas Thermal driver");
242 MODULE_AUTHOR("Pradeep Goudagunta<pgoudagunta@nvidia.com>");
243 MODULE_ALIAS("platform:palmas-thermal");
244 MODULE_LICENSE("GPL v2");