thermal: core: introduce thermal_of_cooling_device_register
[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         unsigned int val;
112         int ret;
113
114         /*
115          * Necessary action can be taken here
116          * e.g: thermal_zone_device_update(pz->tz_device);
117          * will trigger orderly power down sequence.
118          */
119         ret = palmas_read(ptherm_zone->palmas, PALMAS_INTERRUPT_BASE,
120                         PALMAS_INT1_LINE_STATE, &val);
121         if (ret < 0) {
122                 dev_err(ptherm_zone->dev,
123                         "%s: Failed to read INT1_LINE_STATE, %d\n",
124                         __func__, ret);
125         } else {
126                 ptherm_zone->is_crit_temp =
127                                 val & PALMAS_INT1_STATUS_HOTDIE ? 1 : 0;
128                 dev_info(ptherm_zone->dev, "%s: HOTDIE line is equal to %d\n",
129                         __func__, ptherm_zone->is_crit_temp);
130         }
131
132         return IRQ_HANDLED;
133 }
134
135 static int palmas_thermal_probe(struct platform_device *pdev)
136 {
137         struct palmas_therm_zone *ptherm_zone;
138         struct palmas_platform_data *pdata;
139         struct palmas *palmas = dev_get_drvdata(pdev->dev.parent);
140         char *default_tz_name = "palmas-junc-tz";
141         int ret;
142         u8 val;
143
144         pdata = dev_get_platdata(pdev->dev.parent);
145
146         if (!pdata || !(pdata->hd_threshold_temp)) {
147                 dev_err(&pdev->dev, "No platform data\n");
148                 return -ENODEV;
149         }
150
151         ptherm_zone = devm_kzalloc(&pdev->dev, sizeof(*ptherm_zone),
152                         GFP_KERNEL);
153         if (!ptherm_zone) {
154                 dev_err(&pdev->dev, "No available free memory\n");
155                 return -ENOMEM;
156         }
157
158         platform_set_drvdata(pdev, ptherm_zone);
159         ptherm_zone->dev = &pdev->dev;
160         ptherm_zone->palmas = palmas;
161         if (!(pdata->tz_name))
162                 pdata->tz_name = default_tz_name;
163
164         ptherm_zone->tz_device = thermal_zone_device_register(pdata->tz_name,
165                                         1, 0, ptherm_zone, &palmas_tz_ops,
166                                         NULL, 0, 0);
167         if (IS_ERR_OR_NULL(ptherm_zone->tz_device)) {
168                 dev_err(ptherm_zone->dev,
169                         "Register thermal zone device failed.\n");
170                 return PTR_ERR(ptherm_zone->tz_device);
171         }
172
173         palmas_tpoint.temp = pdata->hd_threshold_temp;
174
175         ptherm_zone->irq = platform_get_irq(pdev, 0);
176         ret = request_threaded_irq(ptherm_zone->irq, NULL,
177                         palmas_thermal_irq,
178                         IRQF_ONESHOT, dev_name(&pdev->dev),
179                         ptherm_zone);
180         if (ret < 0) {
181                 dev_err(&pdev->dev,
182                         "request irq %d failed: %dn", ptherm_zone->irq, ret);
183                 goto int_req_failed;
184         }
185
186         switch (palmas_tpoint.temp) {
187         case 108000:
188                 val = 0;
189                 break;
190         case 112000:
191                 val = 1;
192                 break;
193         case 116000:
194                 val = 2;
195                 break;
196         case 120000:
197                 val = 3;
198                 break;
199         default:
200                 dev_err(&pdev->dev, "%ld threshold is not supported",
201                                 palmas_tpoint.temp);
202                 ret = -EINVAL;
203                 goto error;
204         }
205
206         val <<= PALMAS_OSC_THERM_CTRL_THERM_HD_SEL_SHIFT;
207         ret = palmas_update_bits(palmas, PALMAS_PMU_CONTROL_BASE,
208                         PALMAS_OSC_THERM_CTRL,
209                         PALMAS_OSC_THERM_CTRL_THERM_HD_SEL_MASK, val);
210         if (ret < 0) {
211                 dev_err(&pdev->dev, "osc_therm_ctrl reg update failed.\n");
212                 goto error;
213         }
214
215         return 0;
216
217 error:
218         free_irq(ptherm_zone->irq, ptherm_zone);
219 int_req_failed:
220         thermal_zone_device_unregister(ptherm_zone->tz_device);
221         return ret;
222 }
223
224 static int palmas_thermal_remove(struct platform_device *pdev)
225 {
226         struct palmas_therm_zone *ptherm_zone = platform_get_drvdata(pdev);
227
228         thermal_zone_device_unregister(ptherm_zone->tz_device);
229         free_irq(ptherm_zone->irq, ptherm_zone);
230         kfree(ptherm_zone);
231         return 0;
232 }
233
234 static struct platform_driver palmas_thermal_driver = {
235         .probe = palmas_thermal_probe,
236         .remove = palmas_thermal_remove,
237         .driver = {
238                 .name = "palmas-thermal",
239                 .owner = THIS_MODULE,
240         },
241 };
242
243 static int __init palmas_thermal_init(void)
244 {
245         return platform_driver_register(&palmas_thermal_driver);
246 }
247 module_init(palmas_thermal_init);
248
249 static void __exit palmas_thermal_exit(void)
250 {
251         platform_driver_unregister(&palmas_thermal_driver);
252 }
253 module_exit(palmas_thermal_exit);
254
255 MODULE_DESCRIPTION("Palmas Thermal driver");
256 MODULE_AUTHOR("Pradeep Goudagunta<pgoudagunta@nvidia.com>");
257 MODULE_ALIAS("platform:palmas-thermal");
258 MODULE_LICENSE("GPL v2");