regulator: qcom_rpm: Don't update vreg->uV/mV if rpm_reg_write fails
[cascardo/linux.git] / drivers / thermal / int340x_thermal / acpi_thermal_rel.c
1 /* acpi_thermal_rel.c driver for exporting ACPI thermal relationship
2  *
3  * Copyright (c) 2014 Intel Corp
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 as published by
7  * the Free Software Foundation.
8  *
9  */
10
11 /*
12  * Two functionalities included:
13  * 1. Export _TRT, _ART, via misc device interface to the userspace.
14  * 2. Provide parsing result to kernel drivers
15  *
16  */
17 #include <linux/init.h>
18 #include <linux/export.h>
19 #include <linux/module.h>
20 #include <linux/device.h>
21 #include <linux/platform_device.h>
22 #include <linux/io.h>
23 #include <linux/acpi.h>
24 #include <linux/uaccess.h>
25 #include <linux/miscdevice.h>
26 #include "acpi_thermal_rel.h"
27
28 static acpi_handle acpi_thermal_rel_handle;
29 static DEFINE_SPINLOCK(acpi_thermal_rel_chrdev_lock);
30 static int acpi_thermal_rel_chrdev_count;       /* #times opened */
31 static int acpi_thermal_rel_chrdev_exclu;       /* already open exclusive? */
32
33 static int acpi_thermal_rel_open(struct inode *inode, struct file *file)
34 {
35         spin_lock(&acpi_thermal_rel_chrdev_lock);
36         if (acpi_thermal_rel_chrdev_exclu ||
37             (acpi_thermal_rel_chrdev_count && (file->f_flags & O_EXCL))) {
38                 spin_unlock(&acpi_thermal_rel_chrdev_lock);
39                 return -EBUSY;
40         }
41
42         if (file->f_flags & O_EXCL)
43                 acpi_thermal_rel_chrdev_exclu = 1;
44         acpi_thermal_rel_chrdev_count++;
45
46         spin_unlock(&acpi_thermal_rel_chrdev_lock);
47
48         return nonseekable_open(inode, file);
49 }
50
51 static int acpi_thermal_rel_release(struct inode *inode, struct file *file)
52 {
53         spin_lock(&acpi_thermal_rel_chrdev_lock);
54         acpi_thermal_rel_chrdev_count--;
55         acpi_thermal_rel_chrdev_exclu = 0;
56         spin_unlock(&acpi_thermal_rel_chrdev_lock);
57
58         return 0;
59 }
60
61 /**
62  * acpi_parse_trt - Thermal Relationship Table _TRT for passive cooling
63  *
64  * @handle: ACPI handle of the device contains _TRT
65  * @art_count: the number of valid entries resulted from parsing _TRT
66  * @artp: pointer to pointer of array of art entries in parsing result
67  * @create_dev: whether to create platform devices for target and source
68  *
69  */
70 int acpi_parse_trt(acpi_handle handle, int *trt_count, struct trt **trtp,
71                 bool create_dev)
72 {
73         acpi_status status;
74         int result = 0;
75         int i;
76         int nr_bad_entries = 0;
77         struct trt *trts;
78         struct acpi_device *adev;
79         union acpi_object *p;
80         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
81         struct acpi_buffer element = { 0, NULL };
82         struct acpi_buffer trt_format = { sizeof("RRNNNNNN"), "RRNNNNNN" };
83
84         if (!acpi_has_method(handle, "_TRT"))
85                 return 0;
86
87         status = acpi_evaluate_object(handle, "_TRT", NULL, &buffer);
88         if (ACPI_FAILURE(status))
89                 return -ENODEV;
90
91         p = buffer.pointer;
92         if (!p || (p->type != ACPI_TYPE_PACKAGE)) {
93                 pr_err("Invalid _TRT data\n");
94                 result = -EFAULT;
95                 goto end;
96         }
97
98         *trt_count = p->package.count;
99         trts = kzalloc(*trt_count * sizeof(struct trt), GFP_KERNEL);
100         if (!trts) {
101                 result = -ENOMEM;
102                 goto end;
103         }
104
105         for (i = 0; i < *trt_count; i++) {
106                 struct trt *trt = &trts[i - nr_bad_entries];
107
108                 element.length = sizeof(struct trt);
109                 element.pointer = trt;
110
111                 status = acpi_extract_package(&(p->package.elements[i]),
112                                               &trt_format, &element);
113                 if (ACPI_FAILURE(status)) {
114                         nr_bad_entries++;
115                         pr_warn("_TRT package %d is invalid, ignored\n", i);
116                         continue;
117                 }
118                 if (!create_dev)
119                         continue;
120
121                 result = acpi_bus_get_device(trt->source, &adev);
122                 if (!result)
123                         acpi_create_platform_device(adev);
124                 else
125                         pr_warn("Failed to get source ACPI device\n");
126
127                 result = acpi_bus_get_device(trt->target, &adev);
128                 if (!result)
129                         acpi_create_platform_device(adev);
130                 else
131                         pr_warn("Failed to get target ACPI device\n");
132         }
133
134         result = 0;
135
136         *trtp = trts;
137         /* don't count bad entries */
138         *trt_count -= nr_bad_entries;
139 end:
140         kfree(buffer.pointer);
141         return result;
142 }
143 EXPORT_SYMBOL(acpi_parse_trt);
144
145 /**
146  * acpi_parse_art - Parse Active Relationship Table _ART
147  *
148  * @handle: ACPI handle of the device contains _ART
149  * @art_count: the number of valid entries resulted from parsing _ART
150  * @artp: pointer to pointer of array of art entries in parsing result
151  * @create_dev: whether to create platform devices for target and source
152  *
153  */
154 int acpi_parse_art(acpi_handle handle, int *art_count, struct art **artp,
155                 bool create_dev)
156 {
157         acpi_status status;
158         int result = 0;
159         int i;
160         int nr_bad_entries = 0;
161         struct art *arts;
162         struct acpi_device *adev;
163         union acpi_object *p;
164         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
165         struct acpi_buffer element = { 0, NULL };
166         struct acpi_buffer art_format = {
167                 sizeof("RRNNNNNNNNNNN"), "RRNNNNNNNNNNN" };
168
169         if (!acpi_has_method(handle, "_ART"))
170                 return 0;
171
172         status = acpi_evaluate_object(handle, "_ART", NULL, &buffer);
173         if (ACPI_FAILURE(status))
174                 return -ENODEV;
175
176         p = buffer.pointer;
177         if (!p || (p->type != ACPI_TYPE_PACKAGE)) {
178                 pr_err("Invalid _ART data\n");
179                 result = -EFAULT;
180                 goto end;
181         }
182
183         /* ignore p->package.elements[0], as this is _ART Revision field */
184         *art_count = p->package.count - 1;
185         arts = kzalloc(*art_count * sizeof(struct art), GFP_KERNEL);
186         if (!arts) {
187                 result = -ENOMEM;
188                 goto end;
189         }
190
191         for (i = 0; i < *art_count; i++) {
192                 struct art *art = &arts[i - nr_bad_entries];
193
194                 element.length = sizeof(struct art);
195                 element.pointer = art;
196
197                 status = acpi_extract_package(&(p->package.elements[i + 1]),
198                                               &art_format, &element);
199                 if (ACPI_FAILURE(status)) {
200                         pr_warn("_ART package %d is invalid, ignored", i);
201                         nr_bad_entries++;
202                         continue;
203                 }
204                 if (!create_dev)
205                         continue;
206
207                 if (art->source) {
208                         result = acpi_bus_get_device(art->source, &adev);
209                         if (!result)
210                                 acpi_create_platform_device(adev);
211                         else
212                                 pr_warn("Failed to get source ACPI device\n");
213                 }
214                 if (art->target) {
215                         result = acpi_bus_get_device(art->target, &adev);
216                         if (!result)
217                                 acpi_create_platform_device(adev);
218                         else
219                                 pr_warn("Failed to get source ACPI device\n");
220                 }
221         }
222
223         *artp = arts;
224         /* don't count bad entries */
225         *art_count -= nr_bad_entries;
226 end:
227         kfree(buffer.pointer);
228         return result;
229 }
230 EXPORT_SYMBOL(acpi_parse_art);
231
232
233 /* get device name from acpi handle */
234 static void get_single_name(acpi_handle handle, char *name)
235 {
236         struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER};
237
238         if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)))
239                 pr_warn("Failed get name from handle\n");
240         else {
241                 memcpy(name, buffer.pointer, ACPI_NAME_SIZE);
242                 kfree(buffer.pointer);
243         }
244 }
245
246 static int fill_art(char __user *ubuf)
247 {
248         int i;
249         int ret;
250         int count;
251         int art_len;
252         struct art *arts = NULL;
253         union art_object *art_user;
254
255         ret = acpi_parse_art(acpi_thermal_rel_handle, &count, &arts, false);
256         if (ret)
257                 goto free_art;
258         art_len = count * sizeof(union art_object);
259         art_user = kzalloc(art_len, GFP_KERNEL);
260         if (!art_user) {
261                 ret = -ENOMEM;
262                 goto free_art;
263         }
264         /* now fill in user art data */
265         for (i = 0; i < count; i++) {
266                 /* userspace art needs device name instead of acpi reference */
267                 get_single_name(arts[i].source, art_user[i].source_device);
268                 get_single_name(arts[i].target, art_user[i].target_device);
269                 /* copy the rest int data in addition to source and target */
270                 memcpy(&art_user[i].weight, &arts[i].weight,
271                         sizeof(u64) * (ACPI_NR_ART_ELEMENTS - 2));
272         }
273
274         if (copy_to_user(ubuf, art_user, art_len))
275                 ret = -EFAULT;
276         kfree(art_user);
277 free_art:
278         kfree(arts);
279         return ret;
280 }
281
282 static int fill_trt(char __user *ubuf)
283 {
284         int i;
285         int ret;
286         int count;
287         int trt_len;
288         struct trt *trts = NULL;
289         union trt_object *trt_user;
290
291         ret = acpi_parse_trt(acpi_thermal_rel_handle, &count, &trts, false);
292         if (ret)
293                 goto free_trt;
294         trt_len = count * sizeof(union trt_object);
295         trt_user = kzalloc(trt_len, GFP_KERNEL);
296         if (!trt_user) {
297                 ret = -ENOMEM;
298                 goto free_trt;
299         }
300         /* now fill in user trt data */
301         for (i = 0; i < count; i++) {
302                 /* userspace trt needs device name instead of acpi reference */
303                 get_single_name(trts[i].source, trt_user[i].source_device);
304                 get_single_name(trts[i].target, trt_user[i].target_device);
305                 trt_user[i].sample_period = trts[i].sample_period;
306                 trt_user[i].influence = trts[i].influence;
307         }
308
309         if (copy_to_user(ubuf, trt_user, trt_len))
310                 ret = -EFAULT;
311         kfree(trt_user);
312 free_trt:
313         kfree(trts);
314         return ret;
315 }
316
317 static long acpi_thermal_rel_ioctl(struct file *f, unsigned int cmd,
318                                    unsigned long __arg)
319 {
320         int ret = 0;
321         unsigned long length = 0;
322         int count = 0;
323         char __user *arg = (void __user *)__arg;
324         struct trt *trts;
325         struct art *arts;
326
327         switch (cmd) {
328         case ACPI_THERMAL_GET_TRT_COUNT:
329                 ret = acpi_parse_trt(acpi_thermal_rel_handle, &count,
330                                 &trts, false);
331                 kfree(trts);
332                 if (!ret)
333                         return put_user(count, (unsigned long __user *)__arg);
334                 return ret;
335         case ACPI_THERMAL_GET_TRT_LEN:
336                 ret = acpi_parse_trt(acpi_thermal_rel_handle, &count,
337                                 &trts, false);
338                 kfree(trts);
339                 length = count * sizeof(union trt_object);
340                 if (!ret)
341                         return put_user(length, (unsigned long __user *)__arg);
342                 return ret;
343         case ACPI_THERMAL_GET_TRT:
344                 return fill_trt(arg);
345         case ACPI_THERMAL_GET_ART_COUNT:
346                 ret = acpi_parse_art(acpi_thermal_rel_handle, &count,
347                                 &arts, false);
348                 kfree(arts);
349                 if (!ret)
350                         return put_user(count, (unsigned long __user *)__arg);
351                 return ret;
352         case ACPI_THERMAL_GET_ART_LEN:
353                 ret = acpi_parse_art(acpi_thermal_rel_handle, &count,
354                                 &arts, false);
355                 kfree(arts);
356                 length = count * sizeof(union art_object);
357                 if (!ret)
358                         return put_user(length, (unsigned long __user *)__arg);
359                 return ret;
360
361         case ACPI_THERMAL_GET_ART:
362                 return fill_art(arg);
363
364         default:
365                 return -ENOTTY;
366         }
367 }
368
369 static const struct file_operations acpi_thermal_rel_fops = {
370         .owner          = THIS_MODULE,
371         .open           = acpi_thermal_rel_open,
372         .release        = acpi_thermal_rel_release,
373         .unlocked_ioctl = acpi_thermal_rel_ioctl,
374         .llseek         = no_llseek,
375 };
376
377 static struct miscdevice acpi_thermal_rel_misc_device = {
378         .minor  = MISC_DYNAMIC_MINOR,
379         "acpi_thermal_rel",
380         &acpi_thermal_rel_fops
381 };
382
383 int acpi_thermal_rel_misc_device_add(acpi_handle handle)
384 {
385         acpi_thermal_rel_handle = handle;
386
387         return misc_register(&acpi_thermal_rel_misc_device);
388 }
389 EXPORT_SYMBOL(acpi_thermal_rel_misc_device_add);
390
391 int acpi_thermal_rel_misc_device_remove(acpi_handle handle)
392 {
393         misc_deregister(&acpi_thermal_rel_misc_device);
394
395         return 0;
396 }
397 EXPORT_SYMBOL(acpi_thermal_rel_misc_device_remove);
398
399 MODULE_AUTHOR("Zhang Rui <rui.zhang@intel.com>");
400 MODULE_AUTHOR("Jacob Pan <jacob.jun.pan@intel.com");
401 MODULE_DESCRIPTION("Intel acpi thermal rel misc dev driver");
402 MODULE_LICENSE("GPL v2");