drm: rcar-du: Replace direct DRM encoder access with cast macro
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / acpi / ac.c
1 /*
2  *  acpi_ac.c - ACPI AC Adapter Driver ($Revision: 27 $)
3  *
4  *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6  *
7  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or (at
12  *  your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful, but
15  *  WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  *  General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License along
20  *  with this program; if not, write to the Free Software Foundation, Inc.,
21  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22  *
23  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24  */
25
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/slab.h>
29 #include <linux/init.h>
30 #include <linux/types.h>
31 #include <linux/dmi.h>
32 #include <linux/delay.h>
33 #ifdef CONFIG_ACPI_PROCFS_POWER
34 #include <linux/proc_fs.h>
35 #include <linux/seq_file.h>
36 #endif
37 #include <linux/platform_device.h>
38 #include <linux/power_supply.h>
39 #include <linux/acpi.h>
40
41 #define PREFIX "ACPI: "
42
43 #define ACPI_AC_CLASS                   "ac_adapter"
44 #define ACPI_AC_DEVICE_NAME             "AC Adapter"
45 #define ACPI_AC_FILE_STATE              "state"
46 #define ACPI_AC_NOTIFY_STATUS           0x80
47 #define ACPI_AC_STATUS_OFFLINE          0x00
48 #define ACPI_AC_STATUS_ONLINE           0x01
49 #define ACPI_AC_STATUS_UNKNOWN          0xFF
50
51 #define _COMPONENT              ACPI_AC_COMPONENT
52 ACPI_MODULE_NAME("ac");
53
54 MODULE_AUTHOR("Paul Diefenbaugh");
55 MODULE_DESCRIPTION("ACPI AC Adapter Driver");
56 MODULE_LICENSE("GPL");
57
58
59 static int acpi_ac_add(struct acpi_device *device);
60 static int acpi_ac_remove(struct acpi_device *device);
61 static void acpi_ac_notify(struct acpi_device *device, u32 event);
62
63 static const struct acpi_device_id ac_device_ids[] = {
64         {"ACPI0003", 0},
65         {"", 0},
66 };
67 MODULE_DEVICE_TABLE(acpi, ac_device_ids);
68
69 #ifdef CONFIG_PM_SLEEP
70 static int acpi_ac_resume(struct device *dev);
71 #endif
72 static SIMPLE_DEV_PM_OPS(acpi_ac_pm, NULL, acpi_ac_resume);
73
74 #ifdef CONFIG_ACPI_PROCFS_POWER
75 extern struct proc_dir_entry *acpi_lock_ac_dir(void);
76 extern void *acpi_unlock_ac_dir(struct proc_dir_entry *acpi_ac_dir);
77 static int acpi_ac_open_fs(struct inode *inode, struct file *file);
78 #endif
79
80
81 static int ac_sleep_before_get_state_ms;
82
83 static struct acpi_driver acpi_ac_driver = {
84         .name = "ac",
85         .class = ACPI_AC_CLASS,
86         .ids = ac_device_ids,
87         .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
88         .ops = {
89                 .add = acpi_ac_add,
90                 .remove = acpi_ac_remove,
91                 .notify = acpi_ac_notify,
92                 },
93         .drv.pm = &acpi_ac_pm,
94 };
95
96 struct acpi_ac {
97         struct power_supply charger;
98         struct acpi_device * device;
99         unsigned long long state;
100 };
101
102 #define to_acpi_ac(x) container_of(x, struct acpi_ac, charger)
103
104 #ifdef CONFIG_ACPI_PROCFS_POWER
105 static const struct file_operations acpi_ac_fops = {
106         .owner = THIS_MODULE,
107         .open = acpi_ac_open_fs,
108         .read = seq_read,
109         .llseek = seq_lseek,
110         .release = single_release,
111 };
112 #endif
113
114 /* --------------------------------------------------------------------------
115                                AC Adapter Management
116    -------------------------------------------------------------------------- */
117
118 static int acpi_ac_get_state(struct acpi_ac *ac)
119 {
120         acpi_status status = AE_OK;
121
122         if (!ac)
123                 return -EINVAL;
124
125         status = acpi_evaluate_integer(ac->device->handle, "_PSR", NULL,
126                                        &ac->state);
127         if (ACPI_FAILURE(status)) {
128                 ACPI_EXCEPTION((AE_INFO, status,
129                                 "Error reading AC Adapter state"));
130                 ac->state = ACPI_AC_STATUS_UNKNOWN;
131                 return -ENODEV;
132         }
133
134         return 0;
135 }
136
137 /* --------------------------------------------------------------------------
138                             sysfs I/F
139    -------------------------------------------------------------------------- */
140 static int get_ac_property(struct power_supply *psy,
141                            enum power_supply_property psp,
142                            union power_supply_propval *val)
143 {
144         struct acpi_ac *ac = to_acpi_ac(psy);
145
146         if (!ac)
147                 return -ENODEV;
148
149         if (acpi_ac_get_state(ac))
150                 return -ENODEV;
151
152         switch (psp) {
153         case POWER_SUPPLY_PROP_ONLINE:
154                 val->intval = ac->state;
155                 break;
156         default:
157                 return -EINVAL;
158         }
159         return 0;
160 }
161
162 static enum power_supply_property ac_props[] = {
163         POWER_SUPPLY_PROP_ONLINE,
164 };
165
166 #ifdef CONFIG_ACPI_PROCFS_POWER
167 /* --------------------------------------------------------------------------
168                               FS Interface (/proc)
169    -------------------------------------------------------------------------- */
170
171 static struct proc_dir_entry *acpi_ac_dir;
172
173 static int acpi_ac_seq_show(struct seq_file *seq, void *offset)
174 {
175         struct acpi_ac *ac = seq->private;
176
177
178         if (!ac)
179                 return 0;
180
181         if (acpi_ac_get_state(ac)) {
182                 seq_puts(seq, "ERROR: Unable to read AC Adapter state\n");
183                 return 0;
184         }
185
186         seq_puts(seq, "state:                   ");
187         switch (ac->state) {
188         case ACPI_AC_STATUS_OFFLINE:
189                 seq_puts(seq, "off-line\n");
190                 break;
191         case ACPI_AC_STATUS_ONLINE:
192                 seq_puts(seq, "on-line\n");
193                 break;
194         default:
195                 seq_puts(seq, "unknown\n");
196                 break;
197         }
198
199         return 0;
200 }
201
202 static int acpi_ac_open_fs(struct inode *inode, struct file *file)
203 {
204         return single_open(file, acpi_ac_seq_show, PDE_DATA(inode));
205 }
206
207 static int acpi_ac_add_fs(struct acpi_ac *ac)
208 {
209         struct proc_dir_entry *entry = NULL;
210
211         printk(KERN_WARNING PREFIX "Deprecated procfs I/F for AC is loaded,"
212                         " please retry with CONFIG_ACPI_PROCFS_POWER cleared\n");
213         if (!acpi_device_dir(ac->device)) {
214                 acpi_device_dir(ac->device) =
215                         proc_mkdir(acpi_device_bid(ac->device), acpi_ac_dir);
216                 if (!acpi_device_dir(ac->device))
217                         return -ENODEV;
218         }
219
220         /* 'state' [R] */
221         entry = proc_create_data(ACPI_AC_FILE_STATE,
222                                  S_IRUGO, acpi_device_dir(ac->device),
223                                  &acpi_ac_fops, ac);
224         if (!entry)
225                 return -ENODEV;
226         return 0;
227 }
228
229 static int acpi_ac_remove_fs(struct acpi_ac *ac)
230 {
231
232         if (acpi_device_dir(ac->device)) {
233                 remove_proc_entry(ACPI_AC_FILE_STATE,
234                                   acpi_device_dir(ac->device));
235                 remove_proc_entry(acpi_device_bid(ac->device), acpi_ac_dir);
236                 acpi_device_dir(ac->device) = NULL;
237         }
238
239         return 0;
240 }
241 #endif
242
243 /* --------------------------------------------------------------------------
244                                    Driver Model
245    -------------------------------------------------------------------------- */
246
247 static void acpi_ac_notify(struct acpi_device *device, u32 event)
248 {
249         struct acpi_ac *ac = acpi_driver_data(device);
250
251         if (!ac)
252                 return;
253
254         switch (event) {
255         default:
256                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
257                                   "Unsupported event [0x%x]\n", event));
258         case ACPI_AC_NOTIFY_STATUS:
259         case ACPI_NOTIFY_BUS_CHECK:
260         case ACPI_NOTIFY_DEVICE_CHECK:
261                 /*
262                  * A buggy BIOS may notify AC first and then sleep for
263                  * a specific time before doing actual operations in the
264                  * EC event handler (_Qxx). This will cause the AC state
265                  * reported by the ACPI event to be incorrect, so wait for a
266                  * specific time for the EC event handler to make progress.
267                  */
268                 if (ac_sleep_before_get_state_ms > 0)
269                         msleep(ac_sleep_before_get_state_ms);
270
271                 acpi_ac_get_state(ac);
272                 acpi_bus_generate_netlink_event(device->pnp.device_class,
273                                                   dev_name(&device->dev), event,
274                                                   (u32) ac->state);
275                 acpi_notifier_call_chain(device, event, (u32) ac->state);
276                 kobject_uevent(&ac->charger.dev->kobj, KOBJ_CHANGE);
277         }
278
279         return;
280 }
281
282 static int thinkpad_e530_quirk(const struct dmi_system_id *d)
283 {
284         ac_sleep_before_get_state_ms = 1000;
285         return 0;
286 }
287
288 static struct dmi_system_id ac_dmi_table[] = {
289         {
290         .callback = thinkpad_e530_quirk,
291         .ident = "thinkpad e530",
292         .matches = {
293                 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
294                 DMI_MATCH(DMI_PRODUCT_NAME, "32597CG"),
295                 },
296         },
297         {},
298 };
299
300 static int acpi_ac_add(struct acpi_device *device)
301 {
302         int result = 0;
303         struct acpi_ac *ac = NULL;
304
305
306         if (!device)
307                 return -EINVAL;
308
309         ac = kzalloc(sizeof(struct acpi_ac), GFP_KERNEL);
310         if (!ac)
311                 return -ENOMEM;
312
313         ac->device = device;
314         strcpy(acpi_device_name(device), ACPI_AC_DEVICE_NAME);
315         strcpy(acpi_device_class(device), ACPI_AC_CLASS);
316         device->driver_data = ac;
317
318         result = acpi_ac_get_state(ac);
319         if (result)
320                 goto end;
321
322         ac->charger.name = acpi_device_bid(device);
323 #ifdef CONFIG_ACPI_PROCFS_POWER
324         result = acpi_ac_add_fs(ac);
325         if (result)
326                 goto end;
327 #endif
328         ac->charger.type = POWER_SUPPLY_TYPE_MAINS;
329         ac->charger.properties = ac_props;
330         ac->charger.num_properties = ARRAY_SIZE(ac_props);
331         ac->charger.get_property = get_ac_property;
332         result = power_supply_register(&ac->device->dev, &ac->charger);
333         if (result)
334                 goto end;
335
336         printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
337                acpi_device_name(device), acpi_device_bid(device),
338                ac->state ? "on-line" : "off-line");
339
340 end:
341         if (result) {
342 #ifdef CONFIG_ACPI_PROCFS_POWER
343                 acpi_ac_remove_fs(ac);
344 #endif
345                 kfree(ac);
346         }
347
348         dmi_check_system(ac_dmi_table);
349         return result;
350 }
351
352 #ifdef CONFIG_PM_SLEEP
353 static int acpi_ac_resume(struct device *dev)
354 {
355         struct acpi_ac *ac;
356         unsigned old_state;
357
358         if (!dev)
359                 return -EINVAL;
360
361         ac = acpi_driver_data(to_acpi_device(dev));
362         if (!ac)
363                 return -EINVAL;
364
365         old_state = ac->state;
366         if (acpi_ac_get_state(ac))
367                 return 0;
368         if (old_state != ac->state)
369                 kobject_uevent(&ac->charger.dev->kobj, KOBJ_CHANGE);
370         return 0;
371 }
372 #else
373 #define acpi_ac_resume NULL
374 #endif
375
376 static int acpi_ac_remove(struct acpi_device *device)
377 {
378         struct acpi_ac *ac = NULL;
379
380
381         if (!device || !acpi_driver_data(device))
382                 return -EINVAL;
383
384         ac = acpi_driver_data(device);
385
386         if (ac->charger.dev)
387                 power_supply_unregister(&ac->charger);
388
389 #ifdef CONFIG_ACPI_PROCFS_POWER
390         acpi_ac_remove_fs(ac);
391 #endif
392
393         kfree(ac);
394
395         return 0;
396 }
397
398 static int __init acpi_ac_init(void)
399 {
400         int result;
401
402         if (acpi_disabled)
403                 return -ENODEV;
404
405 #ifdef CONFIG_ACPI_PROCFS_POWER
406         acpi_ac_dir = acpi_lock_ac_dir();
407         if (!acpi_ac_dir)
408                 return -ENODEV;
409 #endif
410
411
412         result = acpi_bus_register_driver(&acpi_ac_driver);
413         if (result < 0) {
414 #ifdef CONFIG_ACPI_PROCFS_POWER
415                 acpi_unlock_ac_dir(acpi_ac_dir);
416 #endif
417                 return -ENODEV;
418         }
419
420         return 0;
421 }
422
423 static void __exit acpi_ac_exit(void)
424 {
425         acpi_bus_unregister_driver(&acpi_ac_driver);
426 #ifdef CONFIG_ACPI_PROCFS_POWER
427         acpi_unlock_ac_dir(acpi_ac_dir);
428 #endif
429 }
430 module_init(acpi_ac_init);
431 module_exit(acpi_ac_exit);