18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Eurobraille/Iris power off support.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Eurobraille's Iris machine is a PC with no APM or ACPI support.
68c2ecf20Sopenharmony_ci * It is shutdown by a special I/O sequence which this module provides.
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci *  Copyright (C) Shérab <Sebastien.Hinderer@ens-lyon.org>
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/moduleparam.h>
128c2ecf20Sopenharmony_ci#include <linux/module.h>
138c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
148c2ecf20Sopenharmony_ci#include <linux/kernel.h>
158c2ecf20Sopenharmony_ci#include <linux/errno.h>
168c2ecf20Sopenharmony_ci#include <linux/delay.h>
178c2ecf20Sopenharmony_ci#include <linux/pm.h>
188c2ecf20Sopenharmony_ci#include <asm/io.h>
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#define IRIS_GIO_BASE		0x340
218c2ecf20Sopenharmony_ci#define IRIS_GIO_INPUT		IRIS_GIO_BASE
228c2ecf20Sopenharmony_ci#define IRIS_GIO_OUTPUT		(IRIS_GIO_BASE + 1)
238c2ecf20Sopenharmony_ci#define IRIS_GIO_PULSE		0x80 /* First byte to send */
248c2ecf20Sopenharmony_ci#define IRIS_GIO_REST		0x00 /* Second byte to send */
258c2ecf20Sopenharmony_ci#define IRIS_GIO_NODEV		0xff /* Likely not an Iris */
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
288c2ecf20Sopenharmony_ciMODULE_AUTHOR("Sébastien Hinderer <Sebastien.Hinderer@ens-lyon.org>");
298c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("A power_off handler for Iris devices from EuroBraille");
308c2ecf20Sopenharmony_ciMODULE_SUPPORTED_DEVICE("Eurobraille/Iris");
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cistatic bool force;
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_cimodule_param(force, bool, 0);
358c2ecf20Sopenharmony_ciMODULE_PARM_DESC(force, "Set to one to force poweroff handler installation.");
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_cistatic void (*old_pm_power_off)(void);
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_cistatic void iris_power_off(void)
408c2ecf20Sopenharmony_ci{
418c2ecf20Sopenharmony_ci	outb(IRIS_GIO_PULSE, IRIS_GIO_OUTPUT);
428c2ecf20Sopenharmony_ci	msleep(850);
438c2ecf20Sopenharmony_ci	outb(IRIS_GIO_REST, IRIS_GIO_OUTPUT);
448c2ecf20Sopenharmony_ci}
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci/*
478c2ecf20Sopenharmony_ci * Before installing the power_off handler, try to make sure the OS is
488c2ecf20Sopenharmony_ci * running on an Iris.  Since Iris does not support DMI, this is done
498c2ecf20Sopenharmony_ci * by reading its input port and seeing whether the read value is
508c2ecf20Sopenharmony_ci * meaningful.
518c2ecf20Sopenharmony_ci */
528c2ecf20Sopenharmony_cistatic int iris_probe(struct platform_device *pdev)
538c2ecf20Sopenharmony_ci{
548c2ecf20Sopenharmony_ci	unsigned char status = inb(IRIS_GIO_INPUT);
558c2ecf20Sopenharmony_ci	if (status == IRIS_GIO_NODEV) {
568c2ecf20Sopenharmony_ci		printk(KERN_ERR "This machine does not seem to be an Iris. "
578c2ecf20Sopenharmony_ci			"Power off handler not installed.\n");
588c2ecf20Sopenharmony_ci		return -ENODEV;
598c2ecf20Sopenharmony_ci	}
608c2ecf20Sopenharmony_ci	old_pm_power_off = pm_power_off;
618c2ecf20Sopenharmony_ci	pm_power_off = &iris_power_off;
628c2ecf20Sopenharmony_ci	printk(KERN_INFO "Iris power_off handler installed.\n");
638c2ecf20Sopenharmony_ci	return 0;
648c2ecf20Sopenharmony_ci}
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_cistatic int iris_remove(struct platform_device *pdev)
678c2ecf20Sopenharmony_ci{
688c2ecf20Sopenharmony_ci	pm_power_off = old_pm_power_off;
698c2ecf20Sopenharmony_ci	printk(KERN_INFO "Iris power_off handler uninstalled.\n");
708c2ecf20Sopenharmony_ci	return 0;
718c2ecf20Sopenharmony_ci}
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_cistatic struct platform_driver iris_driver = {
748c2ecf20Sopenharmony_ci	.driver		= {
758c2ecf20Sopenharmony_ci		.name   = "iris",
768c2ecf20Sopenharmony_ci	},
778c2ecf20Sopenharmony_ci	.probe          = iris_probe,
788c2ecf20Sopenharmony_ci	.remove         = iris_remove,
798c2ecf20Sopenharmony_ci};
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_cistatic struct resource iris_resources[] = {
828c2ecf20Sopenharmony_ci	{
838c2ecf20Sopenharmony_ci		.start  = IRIS_GIO_BASE,
848c2ecf20Sopenharmony_ci		.end    = IRIS_GIO_OUTPUT,
858c2ecf20Sopenharmony_ci		.flags  = IORESOURCE_IO,
868c2ecf20Sopenharmony_ci		.name   = "address"
878c2ecf20Sopenharmony_ci	}
888c2ecf20Sopenharmony_ci};
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_cistatic struct platform_device *iris_device;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_cistatic int iris_init(void)
938c2ecf20Sopenharmony_ci{
948c2ecf20Sopenharmony_ci	int ret;
958c2ecf20Sopenharmony_ci	if (force != 1) {
968c2ecf20Sopenharmony_ci		printk(KERN_ERR "The force parameter has not been set to 1."
978c2ecf20Sopenharmony_ci			" The Iris poweroff handler will not be installed.\n");
988c2ecf20Sopenharmony_ci		return -ENODEV;
998c2ecf20Sopenharmony_ci	}
1008c2ecf20Sopenharmony_ci	ret = platform_driver_register(&iris_driver);
1018c2ecf20Sopenharmony_ci	if (ret < 0) {
1028c2ecf20Sopenharmony_ci		printk(KERN_ERR "Failed to register iris platform driver: %d\n",
1038c2ecf20Sopenharmony_ci			ret);
1048c2ecf20Sopenharmony_ci		return ret;
1058c2ecf20Sopenharmony_ci	}
1068c2ecf20Sopenharmony_ci	iris_device = platform_device_register_simple("iris", (-1),
1078c2ecf20Sopenharmony_ci				iris_resources, ARRAY_SIZE(iris_resources));
1088c2ecf20Sopenharmony_ci	if (IS_ERR(iris_device)) {
1098c2ecf20Sopenharmony_ci		printk(KERN_ERR "Failed to register iris platform device\n");
1108c2ecf20Sopenharmony_ci		platform_driver_unregister(&iris_driver);
1118c2ecf20Sopenharmony_ci		return PTR_ERR(iris_device);
1128c2ecf20Sopenharmony_ci	}
1138c2ecf20Sopenharmony_ci	return 0;
1148c2ecf20Sopenharmony_ci}
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_cistatic void iris_exit(void)
1178c2ecf20Sopenharmony_ci{
1188c2ecf20Sopenharmony_ci	platform_device_unregister(iris_device);
1198c2ecf20Sopenharmony_ci	platform_driver_unregister(&iris_driver);
1208c2ecf20Sopenharmony_ci}
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_cimodule_init(iris_init);
1238c2ecf20Sopenharmony_cimodule_exit(iris_exit);
124