Linux Audio

Check our new training course

Loading...
v4.6
 
  1/*
  2 * linux/drivers/parisc/power.c
  3 * HP PARISC soft power switch support driver
  4 *
  5 * Copyright (c) 2001-2007 Helge Deller <deller@gmx.de>
  6 * All rights reserved.
  7 *
  8 *
  9 * Redistribution and use in source and binary forms, with or without
 10 * modification, are permitted provided that the following conditions
 11 * are met:
 12 * 1. Redistributions of source code must retain the above copyright
 13 *    notice, this list of conditions, and the following disclaimer,
 14 *    without modification.
 15 * 2. The name of the author may not be used to endorse or promote products
 16 *    derived from this software without specific prior written permission.
 17 *
 18 * Alternatively, this software may be distributed under the terms of the
 19 * GNU General Public License ("GPL").
 20 *
 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
 25 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 30 *
 
 31 *
 32 *  HINT:
 33 *  Support of the soft power switch button may be enabled or disabled at
 34 *  runtime through the "/proc/sys/kernel/power" procfs entry.
 35 */ 
 36
 37#include <linux/module.h>
 38#include <linux/init.h>
 39#include <linux/kernel.h>
 40#include <linux/notifier.h>
 41#include <linux/reboot.h>
 42#include <linux/sched.h>
 43#include <linux/kthread.h>
 44#include <linux/pm.h>
 45
 46#include <asm/pdc.h>
 47#include <asm/io.h>
 48#include <asm/led.h>
 49
 50#define DRIVER_NAME  "powersw"
 51#define KTHREAD_NAME "kpowerswd"
 52
 53/* how often should the power button be polled ? */
 54#define POWERSWITCH_POLL_PER_SEC 2
 55
 56/* how long does the power button needs to be down until we react ? */
 57#define POWERSWITCH_DOWN_SEC 2
 58
 59/* assembly code to access special registers */
 60/* taken from PCXL ERS page 82 */
 61#define DIAG_CODE(code)		(0x14000000 + ((code)<<5))
 62
 63#define MFCPU_X(rDiagReg, t_ch, t_th, code) \
 64	(DIAG_CODE(code) + ((rDiagReg)<<21) + ((t_ch)<<16) + ((t_th)<<0) )
 65	
 66#define MTCPU(dr, gr)		MFCPU_X(dr, gr,  0, 0x12)       /* move value of gr to dr[dr] */
 67#define MFCPU_C(dr, gr)		MFCPU_X(dr, gr,  0, 0x30)	/* for dr0 and dr8 only ! */
 68#define MFCPU_T(dr, gr)		MFCPU_X(dr,  0, gr, 0xa0)	/* all dr except dr0 and dr8 */
 69	
 70#define __getDIAG(dr) ( { 			\
 71        register unsigned long __res asm("r28");\
 72	 __asm__ __volatile__ (			\
 73		".word %1" : "=&r" (__res) : "i" (MFCPU_T(dr,28) ) \
 74	);					\
 75	__res;					\
 76} )
 77
 78/* local shutdown counter */
 79static int shutdown_timer __read_mostly;
 80
 81/* check, give feedback and start shutdown after one second */
 82static void process_shutdown(void)
 83{
 84	if (shutdown_timer == 0)
 85		printk(KERN_ALERT KTHREAD_NAME ": Shutdown requested...\n");
 86
 87	shutdown_timer++;
 88	
 89	/* wait until the button was pressed for 1 second */
 90	if (shutdown_timer == (POWERSWITCH_DOWN_SEC*POWERSWITCH_POLL_PER_SEC)) {
 91		static const char msg[] = "Shutting down...";
 92		printk(KERN_INFO KTHREAD_NAME ": %s\n", msg);
 93		lcd_print(msg);
 94
 95		/* send kill signal */
 96		if (kill_cad_pid(SIGINT, 1)) {
 97			/* just in case killing init process failed */
 98			if (pm_power_off)
 99				pm_power_off();
100		}
101	}
102}
103
104
105/* main power switch task struct */
106static struct task_struct *power_task;
107
108/* filename in /proc which can be used to enable/disable the power switch */
109#define SYSCTL_FILENAME	"sys/kernel/power"
110
111/* soft power switch enabled/disabled */
112int pwrsw_enabled __read_mostly = 1;
113
114/* main kernel thread worker. It polls the button state */
115static int kpowerswd(void *param)
116{
117	__set_current_state(TASK_RUNNING);
118
119	do {
120		int button_not_pressed;
121		unsigned long soft_power_reg = (unsigned long) param;
122
123		schedule_timeout_interruptible(pwrsw_enabled ? HZ : HZ/POWERSWITCH_POLL_PER_SEC);
124
125		if (unlikely(!pwrsw_enabled))
126			continue;
127
128		if (soft_power_reg) {
129			/*
130			 * Non-Gecko-style machines:
131			 * Check the power switch status which is read from the
132			 * real I/O location at soft_power_reg.
133			 * Bit 31 ("the lowest bit) is the status of the power switch.
134			 * This bit is "1" if the button is NOT pressed.
135			 */
136			button_not_pressed = (gsc_readl(soft_power_reg) & 0x1);
137		} else {
138			/*
139			 * On gecko style machines (e.g. 712/xx and 715/xx) 
140			 * the power switch status is stored in Bit 0 ("the highest bit")
141			 * of CPU diagnose register 25.
142			 * Warning: Some machines never reset the DIAG flag, even if
143			 * the button has been released again.
144			 */
145			button_not_pressed = (__getDIAG(25) & 0x80000000);
146		}
147
148		if (likely(button_not_pressed)) {
149			if (unlikely(shutdown_timer && /* avoid writing if not necessary */
150				shutdown_timer < (POWERSWITCH_DOWN_SEC*POWERSWITCH_POLL_PER_SEC))) {
151				shutdown_timer = 0;
152				printk(KERN_INFO KTHREAD_NAME ": Shutdown request aborted.\n");
153			}
154		} else
155			process_shutdown();
156
157
158	} while (!kthread_should_stop());
159
160	return 0;
161}
162
163
164/*
165 * powerfail interruption handler (irq IRQ_FROM_REGION(CPU_IRQ_REGION)+2) 
166 */
167#if 0
168static void powerfail_interrupt(int code, void *x)
169{
170	printk(KERN_CRIT "POWERFAIL INTERRUPTION !\n");
171	poweroff();
172}
173#endif
174
175
176
177
178/* parisc_panic_event() is called by the panic handler.
179 * As soon as a panic occurs, our tasklets above will not be
180 * executed any longer. This function then re-enables the 
181 * soft-power switch and allows the user to switch off the system
 
 
 
 
 
182 */
183static int parisc_panic_event(struct notifier_block *this,
184		unsigned long event, void *ptr)
185{
186	/* re-enable the soft-power switch */
187	pdc_soft_power_button(0);
188	return NOTIFY_DONE;
189}
190
191static struct notifier_block parisc_panic_block = {
192	.notifier_call	= parisc_panic_event,
193	.priority	= INT_MAX,
194};
195
 
 
 
 
 
 
 
 
196
197static int __init power_init(void)
198{
199	unsigned long ret;
200	unsigned long soft_power_reg;
201
202#if 0
203	request_irq( IRQ_FROM_REGION(CPU_IRQ_REGION)+2, &powerfail_interrupt,
204		0, "powerfail", NULL);
205#endif
206
207	/* enable the soft power switch if possible */
208	ret = pdc_soft_power_info(&soft_power_reg);
209	if (ret == PDC_OK)
210		ret = pdc_soft_power_button(1);
211	if (ret != PDC_OK)
212		soft_power_reg = -1UL;
213	
214	switch (soft_power_reg) {
215	case 0:		printk(KERN_INFO DRIVER_NAME ": Gecko-style soft power switch enabled.\n");
216			break;
217			
218	case -1UL:	printk(KERN_INFO DRIVER_NAME ": Soft power switch support not available.\n");
219			return -ENODEV;
220	
221	default:	printk(KERN_INFO DRIVER_NAME ": Soft power switch at 0x%08lx enabled.\n",
222				soft_power_reg);
223	}
224
225	power_task = kthread_run(kpowerswd, (void*)soft_power_reg, KTHREAD_NAME);
 
 
 
 
 
 
226	if (IS_ERR(power_task)) {
227		printk(KERN_ERR DRIVER_NAME ": thread creation failed.  Driver not loaded.\n");
228		pdc_soft_power_button(0);
229		return -EIO;
230	}
231
232	/* Register a call for panic conditions. */
233	atomic_notifier_chain_register(&panic_notifier_list,
234			&parisc_panic_block);
235
236	return 0;
237}
238
239static void __exit power_exit(void)
240{
241	kthread_stop(power_task);
242
243	atomic_notifier_chain_unregister(&panic_notifier_list,
244			&parisc_panic_block);
245
246	pdc_soft_power_button(0);
247}
248
249arch_initcall(power_init);
250module_exit(power_exit);
251
252
253MODULE_AUTHOR("Helge Deller <deller@gmx.de>");
254MODULE_DESCRIPTION("Soft power switch driver");
255MODULE_LICENSE("Dual BSD/GPL");
v6.13.7
  1/* SPDX-License-Identifier: GPL-2.0-or-later */
  2/*
  3 * HP PARISC soft power switch driver
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  4 *
  5 * Copyright (c) 2001-2023 Helge Deller <deller@gmx.de>
  6 *
  7 *  HINT:
  8 *  Support of the soft power switch button may be enabled or disabled at
  9 *  runtime through the "/proc/sys/kernel/power" procfs entry.
 10 */
 11
 12#include <linux/module.h>
 13#include <linux/init.h>
 14#include <linux/kernel.h>
 15#include <linux/panic_notifier.h>
 16#include <linux/reboot.h>
 17#include <linux/sched/signal.h>
 18#include <linux/kthread.h>
 19#include <linux/pm.h>
 20
 21#include <asm/pdc.h>
 22#include <asm/io.h>
 23#include <asm/led.h>
 24
 25#define DRIVER_NAME  "powersw"
 26#define KTHREAD_NAME "kpowerswd"
 27
 28/* how often should the power button be polled ? */
 29#define POWERSWITCH_POLL_PER_SEC 2
 30
 31/* how long does the power button needs to be down until we react ? */
 32#define POWERSWITCH_DOWN_SEC 2
 33
 34/* assembly code to access special registers */
 35/* taken from PCXL ERS page 82 */
 36#define DIAG_CODE(code)		(0x14000000 + ((code)<<5))
 37
 38#define MFCPU_X(rDiagReg, t_ch, t_th, code) \
 39	(DIAG_CODE(code) + ((rDiagReg)<<21) + ((t_ch)<<16) + ((t_th)<<0) )
 40
 41#define MTCPU(dr, gr)		MFCPU_X(dr, gr,  0, 0x12)       /* move value of gr to dr[dr] */
 42#define MFCPU_C(dr, gr)		MFCPU_X(dr, gr,  0, 0x30)	/* for dr0 and dr8 only ! */
 43#define MFCPU_T(dr, gr)		MFCPU_X(dr,  0, gr, 0xa0)	/* all dr except dr0 and dr8 */
 44
 45#define __getDIAG(dr) ( {			\
 46        register unsigned long __res asm("r28");\
 47	 __asm__ __volatile__ (			\
 48		".word %1" : "=&r" (__res) : "i" (MFCPU_T(dr,28) ) \
 49	);					\
 50	__res;					\
 51} )
 52
 53/* local shutdown counter */
 54static int shutdown_timer __read_mostly;
 55
 56/* check, give feedback and start shutdown after one second */
 57static void process_shutdown(void)
 58{
 59	if (shutdown_timer == 0)
 60		printk(KERN_ALERT KTHREAD_NAME ": Shutdown requested...\n");
 61
 62	shutdown_timer++;
 63
 64	/* wait until the button was pressed for 1 second */
 65	if (shutdown_timer == (POWERSWITCH_DOWN_SEC*POWERSWITCH_POLL_PER_SEC)) {
 66		static const char msg[] = "Shutting down...";
 67		printk(KERN_INFO KTHREAD_NAME ": %s\n", msg);
 68		lcd_print(msg);
 69
 70		/* send kill signal */
 71		if (kill_cad_pid(SIGINT, 1)) {
 72			/* just in case killing init process failed */
 73			machine_power_off();
 
 74		}
 75	}
 76}
 77
 78
 79/* main power switch task struct */
 80static struct task_struct *power_task;
 81
 82/* filename in /proc which can be used to enable/disable the power switch */
 83#define SYSCTL_FILENAME	"sys/kernel/power"
 84
 85/* soft power switch enabled/disabled */
 86int pwrsw_enabled __read_mostly = 1;
 87
 88/* main kernel thread worker. It polls the button state */
 89static int kpowerswd(void *param)
 90{
 91	__set_current_state(TASK_RUNNING);
 92
 93	do {
 94		int button_not_pressed;
 95		unsigned long soft_power_reg = (unsigned long) param;
 96
 97		schedule_timeout_interruptible(pwrsw_enabled ? HZ : HZ/POWERSWITCH_POLL_PER_SEC);
 98
 99		if (unlikely(!pwrsw_enabled))
100			continue;
101
102		if (soft_power_reg) {
103			/*
104			 * Non-Gecko-style machines:
105			 * Check the power switch status which is read from the
106			 * real I/O location at soft_power_reg.
107			 * Bit 31 ("the lowest bit) is the status of the power switch.
108			 * This bit is "1" if the button is NOT pressed.
109			 */
110			button_not_pressed = (gsc_readl(soft_power_reg) & 0x1);
111		} else {
112			/*
113			 * On gecko style machines (e.g. 712/xx and 715/xx)
114			 * the power switch status is stored in Bit 0 ("the highest bit")
115			 * of CPU diagnose register 25.
116			 * Warning: Some machines never reset the DIAG flag, even if
117			 * the button has been released again.
118			 */
119			button_not_pressed = (__getDIAG(25) & 0x80000000);
120		}
121
122		if (likely(button_not_pressed)) {
123			if (unlikely(shutdown_timer && /* avoid writing if not necessary */
124				shutdown_timer < (POWERSWITCH_DOWN_SEC*POWERSWITCH_POLL_PER_SEC))) {
125				shutdown_timer = 0;
126				printk(KERN_INFO KTHREAD_NAME ": Shutdown request aborted.\n");
127			}
128		} else
129			process_shutdown();
130
131
132	} while (!kthread_should_stop());
133
134	return 0;
135}
136
137
138/*
139 * powerfail interruption handler (irq IRQ_FROM_REGION(CPU_IRQ_REGION)+2)
140 */
141#if 0
142static void powerfail_interrupt(int code, void *x)
143{
144	printk(KERN_CRIT "POWERFAIL INTERRUPTION !\n");
145	poweroff();
146}
147#endif
148
149
150
151
152/*
153 * parisc_panic_event() is called by the panic handler.
154 *
155 * As soon as a panic occurs, our tasklets above will not
156 * be executed any longer. This function then re-enables
157 * the soft-power switch and allows the user to switch off
158 * the system. We rely in pdc_soft_power_button_panic()
159 * since this version spin_trylocks (instead of regular
160 * spinlock), preventing deadlocks on panic path.
161 */
162static int parisc_panic_event(struct notifier_block *this,
163		unsigned long event, void *ptr)
164{
165	/* re-enable the soft-power switch */
166	pdc_soft_power_button_panic(0);
167	return NOTIFY_DONE;
168}
169
170static struct notifier_block parisc_panic_block = {
171	.notifier_call	= parisc_panic_event,
172	.priority	= INT_MAX,
173};
174
175/* qemu soft power-off function */
176static int qemu_power_off(struct sys_off_data *data)
177{
178	/* this turns the system off via SeaBIOS */
179	gsc_writel(0, (unsigned long) data->cb_data);
180	pdc_soft_power_button(1);
181	return NOTIFY_DONE;
182}
183
184static int __init power_init(void)
185{
186	unsigned long ret;
187	unsigned long soft_power_reg;
188
189#if 0
190	request_irq( IRQ_FROM_REGION(CPU_IRQ_REGION)+2, &powerfail_interrupt,
191		0, "powerfail", NULL);
192#endif
193
194	/* enable the soft power switch if possible */
195	ret = pdc_soft_power_info(&soft_power_reg);
196	if (ret == PDC_OK)
197		ret = pdc_soft_power_button(1);
198	if (ret != PDC_OK)
199		soft_power_reg = -1UL;
200
201	switch (soft_power_reg) {
202	case 0:		printk(KERN_INFO DRIVER_NAME ": Gecko-style soft power switch enabled.\n");
203			break;
204
205	case -1UL:	printk(KERN_INFO DRIVER_NAME ": Soft power switch support not available.\n");
206			return -ENODEV;
207
208	default:	printk(KERN_INFO DRIVER_NAME ": Soft power switch at 0x%08lx enabled.\n",
209				soft_power_reg);
210	}
211
212	power_task = NULL;
213	if (running_on_qemu && soft_power_reg)
214		register_sys_off_handler(SYS_OFF_MODE_POWER_OFF, SYS_OFF_PRIO_DEFAULT,
215					qemu_power_off, (void *)soft_power_reg);
216	if (!running_on_qemu || soft_power_reg)
217		power_task = kthread_run(kpowerswd, (void*)soft_power_reg,
218					KTHREAD_NAME);
219	if (IS_ERR(power_task)) {
220		printk(KERN_ERR DRIVER_NAME ": thread creation failed.  Driver not loaded.\n");
221		pdc_soft_power_button(0);
222		return -EIO;
223	}
224
225	/* Register a call for panic conditions. */
226	atomic_notifier_chain_register(&panic_notifier_list,
227			&parisc_panic_block);
228
229	return 0;
230}
231
232static void __exit power_exit(void)
233{
234	kthread_stop(power_task);
235
236	atomic_notifier_chain_unregister(&panic_notifier_list,
237			&parisc_panic_block);
238
239	pdc_soft_power_button(0);
240}
241
242arch_initcall(power_init);
243module_exit(power_exit);
244
245
246MODULE_AUTHOR("Helge Deller <deller@gmx.de>");
247MODULE_DESCRIPTION("Soft power switch driver");
248MODULE_LICENSE("Dual BSD/GPL");