1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Power Management and GPIO expander driver for MPC8349E-mITX-compatible MCU
5 * Copyright (c) 2008 MontaVista Software, Inc.
10 #include <linux/kernel.h>
11 #include <linux/mod_devicetable.h>
12 #include <linux/module.h>
13 #include <linux/device.h>
14 #include <linux/mutex.h>
15 #include <linux/i2c.h>
16 #include <linux/gpio/driver.h>
17 #include <linux/slab.h>
18 #include <linux/kthread.h>
19 #include <linux/property.h>
20 #include <linux/reboot.h>
21 #include <asm/machdep.h>
24 * I don't have specifications for the MCU firmware, I found this register
25 * and bits positions by the trial&error method.
27 #define MCU_REG_CTRL 0x20
28 #define MCU_CTRL_POFF 0x40
29 #define MCU_CTRL_BTN 0x80
31 #define MCU_NUM_GPIO 2
35 struct i2c_client *client;
40 static struct mcu *glob_mcu;
42 struct task_struct *shutdown_thread;
43 static int shutdown_thread_fn(void *data)
46 struct mcu *mcu = glob_mcu;
48 while (!kthread_should_stop()) {
49 ret = i2c_smbus_read_byte_data(mcu->client, MCU_REG_CTRL);
51 pr_err("MCU status reg read failed.\n");
55 if (mcu->reg_ctrl & MCU_CTRL_BTN) {
56 i2c_smbus_write_byte_data(mcu->client, MCU_REG_CTRL,
57 mcu->reg_ctrl & ~MCU_CTRL_BTN);
62 set_current_state(TASK_INTERRUPTIBLE);
69 static ssize_t show_status(struct device *d,
70 struct device_attribute *attr, char *buf)
73 struct mcu *mcu = glob_mcu;
75 ret = i2c_smbus_read_byte_data(mcu->client, MCU_REG_CTRL);
80 return sprintf(buf, "%02x\n", ret);
82 static DEVICE_ATTR(status, 0444, show_status, NULL);
84 static void mcu_power_off(void)
86 struct mcu *mcu = glob_mcu;
88 pr_info("Sending power-off request to the MCU...\n");
89 mutex_lock(&mcu->lock);
90 i2c_smbus_write_byte_data(mcu->client, MCU_REG_CTRL,
91 mcu->reg_ctrl | MCU_CTRL_POFF);
92 mutex_unlock(&mcu->lock);
95 static void mcu_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
97 struct mcu *mcu = gpiochip_get_data(gc);
98 u8 bit = 1 << (4 + gpio);
100 mutex_lock(&mcu->lock);
102 mcu->reg_ctrl &= ~bit;
104 mcu->reg_ctrl |= bit;
106 i2c_smbus_write_byte_data(mcu->client, MCU_REG_CTRL, mcu->reg_ctrl);
107 mutex_unlock(&mcu->lock);
110 static int mcu_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
112 mcu_gpio_set(gc, gpio, val);
116 static int mcu_gpiochip_add(struct mcu *mcu)
118 struct device *dev = &mcu->client->dev;
119 struct gpio_chip *gc = &mcu->gc;
121 gc->owner = THIS_MODULE;
122 gc->label = kasprintf(GFP_KERNEL, "%pfw", dev_fwnode(dev));
124 gc->ngpio = MCU_NUM_GPIO;
126 gc->set = mcu_gpio_set;
127 gc->direction_output = mcu_gpio_dir_out;
130 return gpiochip_add_data(gc, mcu);
133 static void mcu_gpiochip_remove(struct mcu *mcu)
135 kfree(mcu->gc.label);
136 gpiochip_remove(&mcu->gc);
139 static int mcu_probe(struct i2c_client *client)
144 mcu = kzalloc(sizeof(*mcu), GFP_KERNEL);
148 mutex_init(&mcu->lock);
149 mcu->client = client;
150 i2c_set_clientdata(client, mcu);
152 ret = i2c_smbus_read_byte_data(mcu->client, MCU_REG_CTRL);
157 ret = mcu_gpiochip_add(mcu);
161 /* XXX: this is potentially racy, but there is no lock for pm_power_off */
164 pm_power_off = mcu_power_off;
165 dev_info(&client->dev, "will provide power-off service\n");
168 if (device_create_file(&client->dev, &dev_attr_status))
169 dev_err(&client->dev,
170 "couldn't create device file for status\n");
172 shutdown_thread = kthread_run(shutdown_thread_fn, NULL,
181 static void mcu_remove(struct i2c_client *client)
183 struct mcu *mcu = i2c_get_clientdata(client);
185 kthread_stop(shutdown_thread);
187 device_remove_file(&client->dev, &dev_attr_status);
189 if (glob_mcu == mcu) {
194 mcu_gpiochip_remove(mcu);
198 static const struct i2c_device_id mcu_ids[] = {
199 { "mcu-mpc8349emitx", },
202 MODULE_DEVICE_TABLE(i2c, mcu_ids);
204 static const struct of_device_id mcu_of_match_table[] = {
205 { .compatible = "fsl,mcu-mpc8349emitx", },
209 static struct i2c_driver mcu_driver = {
211 .name = "mcu-mpc8349emitx",
212 .of_match_table = mcu_of_match_table,
215 .remove = mcu_remove,
219 module_i2c_driver(mcu_driver);
221 MODULE_DESCRIPTION("Power Management and GPIO expander driver for "
222 "MPC8349E-mITX-compatible MCU");
224 MODULE_LICENSE("GPL");