Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *  Lemote loongson2f family machines' specific suspend support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *  Copyright (C) 2009 Lemote Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *  Author: Wu Zhangjin <wuzhangjin@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/suspend.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/pm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/i8042.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <asm/i8259.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <asm/mipsregs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <asm/bootinfo.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <loongson.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <cs5536/cs5536_mfgpt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include "ec_kb3310b.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #define I8042_KBD_IRQ		1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #define I8042_CTR_KBDINT	0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #define I8042_CTR_KBDDIS	0x10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) static unsigned char i8042_ctr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) static int i8042_enable_kbd_port(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	if (i8042_command(&i8042_ctr, I8042_CMD_CTL_RCTR)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 		pr_err("i8042.c: Can't read CTR while enabling i8042 kbd port."
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 		       "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	i8042_ctr &= ~I8042_CTR_KBDDIS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	i8042_ctr |= I8042_CTR_KBDINT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 		i8042_ctr &= ~I8042_CTR_KBDINT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 		i8042_ctr |= I8042_CTR_KBDDIS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		pr_err("i8042.c: Failed to enable KBD port.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) void setup_wakeup_events(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	int irq_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	switch (mips_machtype) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	case MACH_LEMOTE_ML2F7:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	case MACH_LEMOTE_YL2F89:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		/* open the keyboard irq in i8259A */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		outb((0xff & ~(1 << I8042_KBD_IRQ)), PIC_MASTER_IMR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		irq_mask = inb(PIC_MASTER_IMR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		/* enable keyboard port */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		i8042_enable_kbd_port();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		/* Wakeup CPU via SCI lid open event */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		outb(irq_mask & ~(1 << PIC_CASCADE_IR), PIC_MASTER_IMR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		inb(PIC_MASTER_IMR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		outb(0xff & ~(1 << (SCI_IRQ_NUM - 8)), PIC_SLAVE_IMR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		inb(PIC_SLAVE_IMR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) static struct delayed_work lid_task;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) static int initialized;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) /* yeeloong_report_lid_status will be implemented in yeeloong_laptop.c */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) sci_handler yeeloong_report_lid_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) EXPORT_SYMBOL(yeeloong_report_lid_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) static void yeeloong_lid_update_task(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	if (yeeloong_report_lid_status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		yeeloong_report_lid_status(BIT_LID_DETECT_ON);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) int wakeup_loongson(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	int irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	/* query the interrupt number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	irq = mach_i8259_irq();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	if (irq < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	printk(KERN_INFO "%s: irq = %d\n", __func__, irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	if (irq == I8042_KBD_IRQ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	else if (irq == SCI_IRQ_NUM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		int ret, sci_event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		/* query the event number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		ret = ec_query_seq(CMD_GET_EVENT_NUM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		sci_event = ec_get_event_num();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		if (sci_event < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		if (sci_event == EVENT_LID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 			int lid_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 			/* check the LID status */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 			lid_status = ec_read(REG_LID_DETECT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 			/* wakeup cpu when people open the LID */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 			if (lid_status == BIT_LID_DETECT_ON) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 				/* If we call it directly here, the WARNING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 				 * will be sent out by getnstimeofday
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 				 * via "WARN_ON(timekeeping_suspended);"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 				 * because we can not schedule in suspend mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 				if (initialized == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 					INIT_DELAYED_WORK(&lid_task,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 						yeeloong_lid_update_task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 					initialized = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 				schedule_delayed_work(&lid_task, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 				return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) void __weak mach_suspend(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	disable_mfgpt0_counter();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) void __weak mach_resume(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	enable_mfgpt0_counter();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }