Johannes Berg | 70c3967 | 2006-06-21 12:11:16 +0200 | [diff] [blame] | 1 | /* |
| 2 | * via-pmu LED class device |
| 3 | * |
| 4 | * Copyright 2006 Johannes Berg <johannes@sipsolutions.net> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or |
| 14 | * NON INFRINGEMENT. See the GNU General Public License for more |
| 15 | * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | * |
| 21 | */ |
| 22 | #include <linux/types.h> |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/device.h> |
| 25 | #include <linux/leds.h> |
| 26 | #include <linux/adb.h> |
| 27 | #include <linux/pmu.h> |
Christophe Leroy | a486e51 | 2022-04-01 19:15:53 +0200 | [diff] [blame] | 28 | #include <linux/of.h> |
Johannes Berg | 70c3967 | 2006-06-21 12:11:16 +0200 | [diff] [blame] | 29 | |
| 30 | static spinlock_t pmu_blink_lock; |
| 31 | static struct adb_request pmu_blink_req; |
| 32 | /* -1: no change, 0: request off, 1: request on */ |
| 33 | static int requested_change; |
Johannes Berg | 70c3967 | 2006-06-21 12:11:16 +0200 | [diff] [blame] | 34 | |
| 35 | static void pmu_req_done(struct adb_request * req) |
| 36 | { |
| 37 | unsigned long flags; |
| 38 | |
| 39 | spin_lock_irqsave(&pmu_blink_lock, flags); |
| 40 | /* if someone requested a change in the meantime |
| 41 | * (we only see the last one which is fine) |
| 42 | * then apply it now */ |
Johannes Berg | f596575 | 2007-05-08 01:08:00 +1000 | [diff] [blame] | 43 | if (requested_change != -1 && !pmu_sys_suspended) |
Johannes Berg | 70c3967 | 2006-06-21 12:11:16 +0200 | [diff] [blame] | 44 | pmu_request(&pmu_blink_req, NULL, 4, 0xee, 4, 0, requested_change); |
| 45 | /* reset requested change */ |
| 46 | requested_change = -1; |
| 47 | spin_unlock_irqrestore(&pmu_blink_lock, flags); |
| 48 | } |
| 49 | |
| 50 | static void pmu_led_set(struct led_classdev *led_cdev, |
| 51 | enum led_brightness brightness) |
| 52 | { |
| 53 | unsigned long flags; |
| 54 | |
| 55 | spin_lock_irqsave(&pmu_blink_lock, flags); |
| 56 | switch (brightness) { |
| 57 | case LED_OFF: |
| 58 | requested_change = 0; |
| 59 | break; |
| 60 | case LED_FULL: |
| 61 | requested_change = 1; |
| 62 | break; |
| 63 | default: |
| 64 | goto out; |
| 65 | break; |
| 66 | } |
| 67 | /* if request isn't done, then don't do anything */ |
Johannes Berg | f596575 | 2007-05-08 01:08:00 +1000 | [diff] [blame] | 68 | if (pmu_blink_req.complete && !pmu_sys_suspended) |
Johannes Berg | 70c3967 | 2006-06-21 12:11:16 +0200 | [diff] [blame] | 69 | pmu_request(&pmu_blink_req, NULL, 4, 0xee, 4, 0, requested_change); |
| 70 | out: |
| 71 | spin_unlock_irqrestore(&pmu_blink_lock, flags); |
| 72 | } |
| 73 | |
| 74 | static struct led_classdev pmu_led = { |
Olaf Hering | db3f520 | 2009-09-07 14:37:27 +0100 | [diff] [blame] | 75 | .name = "pmu-led::front", |
Stephan Linz | 83e2c70 | 2016-06-10 07:59:58 +0200 | [diff] [blame] | 76 | #ifdef CONFIG_ADB_PMU_LED_DISK |
| 77 | .default_trigger = "disk-activity", |
Johannes Berg | 70c3967 | 2006-06-21 12:11:16 +0200 | [diff] [blame] | 78 | #endif |
| 79 | .brightness_set = pmu_led_set, |
| 80 | }; |
| 81 | |
Johannes Berg | 70c3967 | 2006-06-21 12:11:16 +0200 | [diff] [blame] | 82 | static int __init via_pmu_led_init(void) |
| 83 | { |
| 84 | struct device_node *dt; |
| 85 | const char *model; |
| 86 | |
| 87 | /* only do this on keylargo based models */ |
| 88 | if (pmu_get_model() != PMU_KEYLARGO_BASED) |
| 89 | return -ENODEV; |
| 90 | |
| 91 | dt = of_find_node_by_path("/"); |
| 92 | if (dt == NULL) |
| 93 | return -ENODEV; |
Stephen Rothwell | 01b2726 | 2007-04-27 13:41:15 +1000 | [diff] [blame] | 94 | model = of_get_property(dt, "model", NULL); |
Julia Lawall | 1f7aac6 | 2010-08-28 23:52:40 +0000 | [diff] [blame] | 95 | if (model == NULL) { |
| 96 | of_node_put(dt); |
Johannes Berg | 70c3967 | 2006-06-21 12:11:16 +0200 | [diff] [blame] | 97 | return -ENODEV; |
Julia Lawall | 1f7aac6 | 2010-08-28 23:52:40 +0000 | [diff] [blame] | 98 | } |
Johannes Berg | 70c3967 | 2006-06-21 12:11:16 +0200 | [diff] [blame] | 99 | if (strncmp(model, "PowerBook", strlen("PowerBook")) != 0 && |
Tony Vroon | e51b85d | 2007-03-22 23:31:08 +0000 | [diff] [blame] | 100 | strncmp(model, "iBook", strlen("iBook")) != 0 && |
| 101 | strcmp(model, "PowerMac7,2") != 0 && |
| 102 | strcmp(model, "PowerMac7,3") != 0) { |
Johannes Berg | 70c3967 | 2006-06-21 12:11:16 +0200 | [diff] [blame] | 103 | of_node_put(dt); |
| 104 | /* ignore */ |
| 105 | return -ENODEV; |
| 106 | } |
| 107 | of_node_put(dt); |
| 108 | |
| 109 | spin_lock_init(&pmu_blink_lock); |
| 110 | /* no outstanding req */ |
| 111 | pmu_blink_req.complete = 1; |
| 112 | pmu_blink_req.done = pmu_req_done; |
Johannes Berg | f596575 | 2007-05-08 01:08:00 +1000 | [diff] [blame] | 113 | |
Johannes Berg | 70c3967 | 2006-06-21 12:11:16 +0200 | [diff] [blame] | 114 | return led_classdev_register(NULL, &pmu_led); |
| 115 | } |
| 116 | |
| 117 | late_initcall(via_pmu_led_init); |