18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Power Management and GPIO expander driver for MPC8349E-mITX-compatible MCU
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2008  MontaVista Software, Inc.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/kernel.h>
118c2ecf20Sopenharmony_ci#include <linux/module.h>
128c2ecf20Sopenharmony_ci#include <linux/device.h>
138c2ecf20Sopenharmony_ci#include <linux/mutex.h>
148c2ecf20Sopenharmony_ci#include <linux/i2c.h>
158c2ecf20Sopenharmony_ci#include <linux/gpio/driver.h>
168c2ecf20Sopenharmony_ci#include <linux/of.h>
178c2ecf20Sopenharmony_ci#include <linux/of_gpio.h>
188c2ecf20Sopenharmony_ci#include <linux/slab.h>
198c2ecf20Sopenharmony_ci#include <linux/kthread.h>
208c2ecf20Sopenharmony_ci#include <linux/reboot.h>
218c2ecf20Sopenharmony_ci#include <asm/prom.h>
228c2ecf20Sopenharmony_ci#include <asm/machdep.h>
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci/*
258c2ecf20Sopenharmony_ci * I don't have specifications for the MCU firmware, I found this register
268c2ecf20Sopenharmony_ci * and bits positions by the trial&error method.
278c2ecf20Sopenharmony_ci */
288c2ecf20Sopenharmony_ci#define MCU_REG_CTRL	0x20
298c2ecf20Sopenharmony_ci#define MCU_CTRL_POFF	0x40
308c2ecf20Sopenharmony_ci#define MCU_CTRL_BTN	0x80
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci#define MCU_NUM_GPIO	2
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_cistruct mcu {
358c2ecf20Sopenharmony_ci	struct mutex lock;
368c2ecf20Sopenharmony_ci	struct i2c_client *client;
378c2ecf20Sopenharmony_ci	struct gpio_chip gc;
388c2ecf20Sopenharmony_ci	u8 reg_ctrl;
398c2ecf20Sopenharmony_ci};
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_cistatic struct mcu *glob_mcu;
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistruct task_struct *shutdown_thread;
448c2ecf20Sopenharmony_cistatic int shutdown_thread_fn(void *data)
458c2ecf20Sopenharmony_ci{
468c2ecf20Sopenharmony_ci	int ret;
478c2ecf20Sopenharmony_ci	struct mcu *mcu = glob_mcu;
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	while (!kthread_should_stop()) {
508c2ecf20Sopenharmony_ci		ret = i2c_smbus_read_byte_data(mcu->client, MCU_REG_CTRL);
518c2ecf20Sopenharmony_ci		if (ret < 0)
528c2ecf20Sopenharmony_ci			pr_err("MCU status reg read failed.\n");
538c2ecf20Sopenharmony_ci		mcu->reg_ctrl = ret;
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci		if (mcu->reg_ctrl & MCU_CTRL_BTN) {
578c2ecf20Sopenharmony_ci			i2c_smbus_write_byte_data(mcu->client, MCU_REG_CTRL,
588c2ecf20Sopenharmony_ci						  mcu->reg_ctrl & ~MCU_CTRL_BTN);
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci			ctrl_alt_del();
618c2ecf20Sopenharmony_ci		}
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci		set_current_state(TASK_INTERRUPTIBLE);
648c2ecf20Sopenharmony_ci		schedule_timeout(HZ);
658c2ecf20Sopenharmony_ci	}
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	return 0;
688c2ecf20Sopenharmony_ci}
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_cistatic ssize_t show_status(struct device *d,
718c2ecf20Sopenharmony_ci			   struct device_attribute *attr, char *buf)
728c2ecf20Sopenharmony_ci{
738c2ecf20Sopenharmony_ci	int ret;
748c2ecf20Sopenharmony_ci	struct mcu *mcu = glob_mcu;
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	ret = i2c_smbus_read_byte_data(mcu->client, MCU_REG_CTRL);
778c2ecf20Sopenharmony_ci	if (ret < 0)
788c2ecf20Sopenharmony_ci		return -ENODEV;
798c2ecf20Sopenharmony_ci	mcu->reg_ctrl = ret;
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	return sprintf(buf, "%02x\n", ret);
828c2ecf20Sopenharmony_ci}
838c2ecf20Sopenharmony_cistatic DEVICE_ATTR(status, 0444, show_status, NULL);
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_cistatic void mcu_power_off(void)
868c2ecf20Sopenharmony_ci{
878c2ecf20Sopenharmony_ci	struct mcu *mcu = glob_mcu;
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	pr_info("Sending power-off request to the MCU...\n");
908c2ecf20Sopenharmony_ci	mutex_lock(&mcu->lock);
918c2ecf20Sopenharmony_ci	i2c_smbus_write_byte_data(mcu->client, MCU_REG_CTRL,
928c2ecf20Sopenharmony_ci				  mcu->reg_ctrl | MCU_CTRL_POFF);
938c2ecf20Sopenharmony_ci	mutex_unlock(&mcu->lock);
948c2ecf20Sopenharmony_ci}
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_cistatic void mcu_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
978c2ecf20Sopenharmony_ci{
988c2ecf20Sopenharmony_ci	struct mcu *mcu = gpiochip_get_data(gc);
998c2ecf20Sopenharmony_ci	u8 bit = 1 << (4 + gpio);
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	mutex_lock(&mcu->lock);
1028c2ecf20Sopenharmony_ci	if (val)
1038c2ecf20Sopenharmony_ci		mcu->reg_ctrl &= ~bit;
1048c2ecf20Sopenharmony_ci	else
1058c2ecf20Sopenharmony_ci		mcu->reg_ctrl |= bit;
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	i2c_smbus_write_byte_data(mcu->client, MCU_REG_CTRL, mcu->reg_ctrl);
1088c2ecf20Sopenharmony_ci	mutex_unlock(&mcu->lock);
1098c2ecf20Sopenharmony_ci}
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_cistatic int mcu_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
1128c2ecf20Sopenharmony_ci{
1138c2ecf20Sopenharmony_ci	mcu_gpio_set(gc, gpio, val);
1148c2ecf20Sopenharmony_ci	return 0;
1158c2ecf20Sopenharmony_ci}
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_cistatic int mcu_gpiochip_add(struct mcu *mcu)
1188c2ecf20Sopenharmony_ci{
1198c2ecf20Sopenharmony_ci	struct device_node *np;
1208c2ecf20Sopenharmony_ci	struct gpio_chip *gc = &mcu->gc;
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	np = of_find_compatible_node(NULL, NULL, "fsl,mcu-mpc8349emitx");
1238c2ecf20Sopenharmony_ci	if (!np)
1248c2ecf20Sopenharmony_ci		return -ENODEV;
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	gc->owner = THIS_MODULE;
1278c2ecf20Sopenharmony_ci	gc->label = kasprintf(GFP_KERNEL, "%pOF", np);
1288c2ecf20Sopenharmony_ci	gc->can_sleep = 1;
1298c2ecf20Sopenharmony_ci	gc->ngpio = MCU_NUM_GPIO;
1308c2ecf20Sopenharmony_ci	gc->base = -1;
1318c2ecf20Sopenharmony_ci	gc->set = mcu_gpio_set;
1328c2ecf20Sopenharmony_ci	gc->direction_output = mcu_gpio_dir_out;
1338c2ecf20Sopenharmony_ci	gc->of_node = np;
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	return gpiochip_add_data(gc, mcu);
1368c2ecf20Sopenharmony_ci}
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_cistatic int mcu_gpiochip_remove(struct mcu *mcu)
1398c2ecf20Sopenharmony_ci{
1408c2ecf20Sopenharmony_ci	kfree(mcu->gc.label);
1418c2ecf20Sopenharmony_ci	gpiochip_remove(&mcu->gc);
1428c2ecf20Sopenharmony_ci	return 0;
1438c2ecf20Sopenharmony_ci}
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_cistatic int mcu_probe(struct i2c_client *client)
1468c2ecf20Sopenharmony_ci{
1478c2ecf20Sopenharmony_ci	struct mcu *mcu;
1488c2ecf20Sopenharmony_ci	int ret;
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	mcu = kzalloc(sizeof(*mcu), GFP_KERNEL);
1518c2ecf20Sopenharmony_ci	if (!mcu)
1528c2ecf20Sopenharmony_ci		return -ENOMEM;
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	mutex_init(&mcu->lock);
1558c2ecf20Sopenharmony_ci	mcu->client = client;
1568c2ecf20Sopenharmony_ci	i2c_set_clientdata(client, mcu);
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	ret = i2c_smbus_read_byte_data(mcu->client, MCU_REG_CTRL);
1598c2ecf20Sopenharmony_ci	if (ret < 0)
1608c2ecf20Sopenharmony_ci		goto err;
1618c2ecf20Sopenharmony_ci	mcu->reg_ctrl = ret;
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	ret = mcu_gpiochip_add(mcu);
1648c2ecf20Sopenharmony_ci	if (ret)
1658c2ecf20Sopenharmony_ci		goto err;
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	/* XXX: this is potentially racy, but there is no lock for pm_power_off */
1688c2ecf20Sopenharmony_ci	if (!pm_power_off) {
1698c2ecf20Sopenharmony_ci		glob_mcu = mcu;
1708c2ecf20Sopenharmony_ci		pm_power_off = mcu_power_off;
1718c2ecf20Sopenharmony_ci		dev_info(&client->dev, "will provide power-off service\n");
1728c2ecf20Sopenharmony_ci	}
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	if (device_create_file(&client->dev, &dev_attr_status))
1758c2ecf20Sopenharmony_ci		dev_err(&client->dev,
1768c2ecf20Sopenharmony_ci			"couldn't create device file for status\n");
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	shutdown_thread = kthread_run(shutdown_thread_fn, NULL,
1798c2ecf20Sopenharmony_ci				      "mcu-i2c-shdn");
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	return 0;
1828c2ecf20Sopenharmony_cierr:
1838c2ecf20Sopenharmony_ci	kfree(mcu);
1848c2ecf20Sopenharmony_ci	return ret;
1858c2ecf20Sopenharmony_ci}
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_cistatic int mcu_remove(struct i2c_client *client)
1888c2ecf20Sopenharmony_ci{
1898c2ecf20Sopenharmony_ci	struct mcu *mcu = i2c_get_clientdata(client);
1908c2ecf20Sopenharmony_ci	int ret;
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	kthread_stop(shutdown_thread);
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	device_remove_file(&client->dev, &dev_attr_status);
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	if (glob_mcu == mcu) {
1978c2ecf20Sopenharmony_ci		pm_power_off = NULL;
1988c2ecf20Sopenharmony_ci		glob_mcu = NULL;
1998c2ecf20Sopenharmony_ci	}
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	ret = mcu_gpiochip_remove(mcu);
2028c2ecf20Sopenharmony_ci	if (ret)
2038c2ecf20Sopenharmony_ci		return ret;
2048c2ecf20Sopenharmony_ci	kfree(mcu);
2058c2ecf20Sopenharmony_ci	return 0;
2068c2ecf20Sopenharmony_ci}
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_cistatic const struct i2c_device_id mcu_ids[] = {
2098c2ecf20Sopenharmony_ci	{ "mcu-mpc8349emitx", },
2108c2ecf20Sopenharmony_ci	{},
2118c2ecf20Sopenharmony_ci};
2128c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, mcu_ids);
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_cistatic const struct of_device_id mcu_of_match_table[] = {
2158c2ecf20Sopenharmony_ci	{ .compatible = "fsl,mcu-mpc8349emitx", },
2168c2ecf20Sopenharmony_ci	{ },
2178c2ecf20Sopenharmony_ci};
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_cistatic struct i2c_driver mcu_driver = {
2208c2ecf20Sopenharmony_ci	.driver = {
2218c2ecf20Sopenharmony_ci		.name = "mcu-mpc8349emitx",
2228c2ecf20Sopenharmony_ci		.of_match_table = mcu_of_match_table,
2238c2ecf20Sopenharmony_ci	},
2248c2ecf20Sopenharmony_ci	.probe_new = mcu_probe,
2258c2ecf20Sopenharmony_ci	.remove	= mcu_remove,
2268c2ecf20Sopenharmony_ci	.id_table = mcu_ids,
2278c2ecf20Sopenharmony_ci};
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_cimodule_i2c_driver(mcu_driver);
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Power Management and GPIO expander driver for "
2328c2ecf20Sopenharmony_ci		   "MPC8349E-mITX-compatible MCU");
2338c2ecf20Sopenharmony_ciMODULE_AUTHOR("Anton Vorontsov <avorontsov@ru.mvista.com>");
2348c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
235