2 * Power Management and GPIO expander driver for MPC8349E-mITX-compatible MCU
4 * Copyright (c) 2008 MontaVista Software, Inc.
6 * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/device.h>
17 #include <linux/mutex.h>
18 #include <linux/i2c.h>
19 #include <linux/gpio.h>
21 #include <linux/of_gpio.h>
22 #include <linux/slab.h>
23 #include <linux/kthread.h>
24 #include <linux/reboot.h>
26 #include <asm/machdep.h>
29 * I don't have specifications for the MCU firmware, I found this register
30 * and bits positions by the trial&error method.
32 #define MCU_REG_CTRL 0x20
33 #define MCU_CTRL_POFF 0x40
34 #define MCU_CTRL_BTN 0x80
36 #define MCU_NUM_GPIO 2
40 struct i2c_client *client;
45 static struct mcu *glob_mcu;
47 struct task_struct *shutdown_thread;
48 static int shutdown_thread_fn(void *data)
51 struct mcu *mcu = glob_mcu;
53 while (!kthread_should_stop()) {
54 ret = i2c_smbus_read_byte_data(mcu->client, MCU_REG_CTRL);
56 pr_err("MCU status reg read failed.\n");
60 if (mcu->reg_ctrl & MCU_CTRL_BTN) {
61 i2c_smbus_write_byte_data(mcu->client, MCU_REG_CTRL,
62 mcu->reg_ctrl & ~MCU_CTRL_BTN);
67 set_current_state(TASK_INTERRUPTIBLE);
74 static ssize_t show_status(struct device *d,
75 struct device_attribute *attr, char *buf)
78 struct mcu *mcu = glob_mcu;
80 ret = i2c_smbus_read_byte_data(mcu->client, MCU_REG_CTRL);
85 return sprintf(buf, "%02x\n", ret);
87 static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
89 static void mcu_power_off(void)
91 struct mcu *mcu = glob_mcu;
93 pr_info("Sending power-off request to the MCU...\n");
94 mutex_lock(&mcu->lock);
95 i2c_smbus_write_byte_data(mcu->client, MCU_REG_CTRL,
96 mcu->reg_ctrl | MCU_CTRL_POFF);
97 mutex_unlock(&mcu->lock);
100 static void mcu_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
102 struct mcu *mcu = container_of(gc, struct mcu, gc);
103 u8 bit = 1 << (4 + gpio);
105 mutex_lock(&mcu->lock);
107 mcu->reg_ctrl &= ~bit;
109 mcu->reg_ctrl |= bit;
111 i2c_smbus_write_byte_data(mcu->client, MCU_REG_CTRL, mcu->reg_ctrl);
112 mutex_unlock(&mcu->lock);
115 static int mcu_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
117 mcu_gpio_set(gc, gpio, val);
121 static int mcu_gpiochip_add(struct mcu *mcu)
123 struct device_node *np;
124 struct gpio_chip *gc = &mcu->gc;
126 np = of_find_compatible_node(NULL, NULL, "fsl,mcu-mpc8349emitx");
130 gc->owner = THIS_MODULE;
131 gc->label = np->full_name;
133 gc->ngpio = MCU_NUM_GPIO;
135 gc->set = mcu_gpio_set;
136 gc->direction_output = mcu_gpio_dir_out;
139 return gpiochip_add(gc);
142 static int mcu_gpiochip_remove(struct mcu *mcu)
144 return gpiochip_remove(&mcu->gc);
147 static int mcu_probe(struct i2c_client *client, const struct i2c_device_id *id)
152 mcu = kzalloc(sizeof(*mcu), GFP_KERNEL);
156 mutex_init(&mcu->lock);
157 mcu->client = client;
158 i2c_set_clientdata(client, mcu);
160 ret = i2c_smbus_read_byte_data(mcu->client, MCU_REG_CTRL);
165 ret = mcu_gpiochip_add(mcu);
169 /* XXX: this is potentially racy, but there is no lock for ppc_md */
170 if (!ppc_md.power_off) {
172 ppc_md.power_off = mcu_power_off;
173 dev_info(&client->dev, "will provide power-off service\n");
176 if (device_create_file(&client->dev, &dev_attr_status))
177 dev_err(&client->dev,
178 "couldn't create device file for status\n");
180 shutdown_thread = kthread_run(shutdown_thread_fn, NULL,
189 static int mcu_remove(struct i2c_client *client)
191 struct mcu *mcu = i2c_get_clientdata(client);
194 kthread_stop(shutdown_thread);
196 device_remove_file(&client->dev, &dev_attr_status);
198 if (glob_mcu == mcu) {
199 ppc_md.power_off = NULL;
203 ret = mcu_gpiochip_remove(mcu);
210 static const struct i2c_device_id mcu_ids[] = {
211 { "mcu-mpc8349emitx", },
214 MODULE_DEVICE_TABLE(i2c, mcu_ids);
216 static struct of_device_id mcu_of_match_table[] = {
217 { .compatible = "fsl,mcu-mpc8349emitx", },
221 static struct i2c_driver mcu_driver = {
223 .name = "mcu-mpc8349emitx",
224 .owner = THIS_MODULE,
225 .of_match_table = mcu_of_match_table,
228 .remove = mcu_remove,
232 module_i2c_driver(mcu_driver);
234 MODULE_DESCRIPTION("Power Management and GPIO expander driver for "
235 "MPC8349E-mITX-compatible MCU");
236 MODULE_AUTHOR("Anton Vorontsov <avorontsov@ru.mvista.com>");
237 MODULE_LICENSE("GPL");