1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Power Management and GPIO expander driver for MPC8349E-mITX-compatible MCU
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (c) 2008 MontaVista Software, Inc.
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
8*4882a593Smuzhiyun */
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun #include <linux/kernel.h>
11*4882a593Smuzhiyun #include <linux/module.h>
12*4882a593Smuzhiyun #include <linux/device.h>
13*4882a593Smuzhiyun #include <linux/mutex.h>
14*4882a593Smuzhiyun #include <linux/i2c.h>
15*4882a593Smuzhiyun #include <linux/gpio/driver.h>
16*4882a593Smuzhiyun #include <linux/of.h>
17*4882a593Smuzhiyun #include <linux/of_gpio.h>
18*4882a593Smuzhiyun #include <linux/slab.h>
19*4882a593Smuzhiyun #include <linux/kthread.h>
20*4882a593Smuzhiyun #include <linux/reboot.h>
21*4882a593Smuzhiyun #include <asm/prom.h>
22*4882a593Smuzhiyun #include <asm/machdep.h>
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun /*
25*4882a593Smuzhiyun * I don't have specifications for the MCU firmware, I found this register
26*4882a593Smuzhiyun * and bits positions by the trial&error method.
27*4882a593Smuzhiyun */
28*4882a593Smuzhiyun #define MCU_REG_CTRL 0x20
29*4882a593Smuzhiyun #define MCU_CTRL_POFF 0x40
30*4882a593Smuzhiyun #define MCU_CTRL_BTN 0x80
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun #define MCU_NUM_GPIO 2
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun struct mcu {
35*4882a593Smuzhiyun struct mutex lock;
36*4882a593Smuzhiyun struct i2c_client *client;
37*4882a593Smuzhiyun struct gpio_chip gc;
38*4882a593Smuzhiyun u8 reg_ctrl;
39*4882a593Smuzhiyun };
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun static struct mcu *glob_mcu;
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun struct task_struct *shutdown_thread;
shutdown_thread_fn(void * data)44*4882a593Smuzhiyun static int shutdown_thread_fn(void *data)
45*4882a593Smuzhiyun {
46*4882a593Smuzhiyun int ret;
47*4882a593Smuzhiyun struct mcu *mcu = glob_mcu;
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun while (!kthread_should_stop()) {
50*4882a593Smuzhiyun ret = i2c_smbus_read_byte_data(mcu->client, MCU_REG_CTRL);
51*4882a593Smuzhiyun if (ret < 0)
52*4882a593Smuzhiyun pr_err("MCU status reg read failed.\n");
53*4882a593Smuzhiyun mcu->reg_ctrl = ret;
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun if (mcu->reg_ctrl & MCU_CTRL_BTN) {
57*4882a593Smuzhiyun i2c_smbus_write_byte_data(mcu->client, MCU_REG_CTRL,
58*4882a593Smuzhiyun mcu->reg_ctrl & ~MCU_CTRL_BTN);
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun ctrl_alt_del();
61*4882a593Smuzhiyun }
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun set_current_state(TASK_INTERRUPTIBLE);
64*4882a593Smuzhiyun schedule_timeout(HZ);
65*4882a593Smuzhiyun }
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun return 0;
68*4882a593Smuzhiyun }
69*4882a593Smuzhiyun
show_status(struct device * d,struct device_attribute * attr,char * buf)70*4882a593Smuzhiyun static ssize_t show_status(struct device *d,
71*4882a593Smuzhiyun struct device_attribute *attr, char *buf)
72*4882a593Smuzhiyun {
73*4882a593Smuzhiyun int ret;
74*4882a593Smuzhiyun struct mcu *mcu = glob_mcu;
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun ret = i2c_smbus_read_byte_data(mcu->client, MCU_REG_CTRL);
77*4882a593Smuzhiyun if (ret < 0)
78*4882a593Smuzhiyun return -ENODEV;
79*4882a593Smuzhiyun mcu->reg_ctrl = ret;
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun return sprintf(buf, "%02x\n", ret);
82*4882a593Smuzhiyun }
83*4882a593Smuzhiyun static DEVICE_ATTR(status, 0444, show_status, NULL);
84*4882a593Smuzhiyun
mcu_power_off(void)85*4882a593Smuzhiyun static void mcu_power_off(void)
86*4882a593Smuzhiyun {
87*4882a593Smuzhiyun struct mcu *mcu = glob_mcu;
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun pr_info("Sending power-off request to the MCU...\n");
90*4882a593Smuzhiyun mutex_lock(&mcu->lock);
91*4882a593Smuzhiyun i2c_smbus_write_byte_data(mcu->client, MCU_REG_CTRL,
92*4882a593Smuzhiyun mcu->reg_ctrl | MCU_CTRL_POFF);
93*4882a593Smuzhiyun mutex_unlock(&mcu->lock);
94*4882a593Smuzhiyun }
95*4882a593Smuzhiyun
mcu_gpio_set(struct gpio_chip * gc,unsigned int gpio,int val)96*4882a593Smuzhiyun static void mcu_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
97*4882a593Smuzhiyun {
98*4882a593Smuzhiyun struct mcu *mcu = gpiochip_get_data(gc);
99*4882a593Smuzhiyun u8 bit = 1 << (4 + gpio);
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun mutex_lock(&mcu->lock);
102*4882a593Smuzhiyun if (val)
103*4882a593Smuzhiyun mcu->reg_ctrl &= ~bit;
104*4882a593Smuzhiyun else
105*4882a593Smuzhiyun mcu->reg_ctrl |= bit;
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun i2c_smbus_write_byte_data(mcu->client, MCU_REG_CTRL, mcu->reg_ctrl);
108*4882a593Smuzhiyun mutex_unlock(&mcu->lock);
109*4882a593Smuzhiyun }
110*4882a593Smuzhiyun
mcu_gpio_dir_out(struct gpio_chip * gc,unsigned int gpio,int val)111*4882a593Smuzhiyun static int mcu_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
112*4882a593Smuzhiyun {
113*4882a593Smuzhiyun mcu_gpio_set(gc, gpio, val);
114*4882a593Smuzhiyun return 0;
115*4882a593Smuzhiyun }
116*4882a593Smuzhiyun
mcu_gpiochip_add(struct mcu * mcu)117*4882a593Smuzhiyun static int mcu_gpiochip_add(struct mcu *mcu)
118*4882a593Smuzhiyun {
119*4882a593Smuzhiyun struct device_node *np;
120*4882a593Smuzhiyun struct gpio_chip *gc = &mcu->gc;
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun np = of_find_compatible_node(NULL, NULL, "fsl,mcu-mpc8349emitx");
123*4882a593Smuzhiyun if (!np)
124*4882a593Smuzhiyun return -ENODEV;
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun gc->owner = THIS_MODULE;
127*4882a593Smuzhiyun gc->label = kasprintf(GFP_KERNEL, "%pOF", np);
128*4882a593Smuzhiyun gc->can_sleep = 1;
129*4882a593Smuzhiyun gc->ngpio = MCU_NUM_GPIO;
130*4882a593Smuzhiyun gc->base = -1;
131*4882a593Smuzhiyun gc->set = mcu_gpio_set;
132*4882a593Smuzhiyun gc->direction_output = mcu_gpio_dir_out;
133*4882a593Smuzhiyun gc->of_node = np;
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun return gpiochip_add_data(gc, mcu);
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun
mcu_gpiochip_remove(struct mcu * mcu)138*4882a593Smuzhiyun static int mcu_gpiochip_remove(struct mcu *mcu)
139*4882a593Smuzhiyun {
140*4882a593Smuzhiyun kfree(mcu->gc.label);
141*4882a593Smuzhiyun gpiochip_remove(&mcu->gc);
142*4882a593Smuzhiyun return 0;
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun
mcu_probe(struct i2c_client * client)145*4882a593Smuzhiyun static int mcu_probe(struct i2c_client *client)
146*4882a593Smuzhiyun {
147*4882a593Smuzhiyun struct mcu *mcu;
148*4882a593Smuzhiyun int ret;
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun mcu = kzalloc(sizeof(*mcu), GFP_KERNEL);
151*4882a593Smuzhiyun if (!mcu)
152*4882a593Smuzhiyun return -ENOMEM;
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun mutex_init(&mcu->lock);
155*4882a593Smuzhiyun mcu->client = client;
156*4882a593Smuzhiyun i2c_set_clientdata(client, mcu);
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun ret = i2c_smbus_read_byte_data(mcu->client, MCU_REG_CTRL);
159*4882a593Smuzhiyun if (ret < 0)
160*4882a593Smuzhiyun goto err;
161*4882a593Smuzhiyun mcu->reg_ctrl = ret;
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun ret = mcu_gpiochip_add(mcu);
164*4882a593Smuzhiyun if (ret)
165*4882a593Smuzhiyun goto err;
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun /* XXX: this is potentially racy, but there is no lock for pm_power_off */
168*4882a593Smuzhiyun if (!pm_power_off) {
169*4882a593Smuzhiyun glob_mcu = mcu;
170*4882a593Smuzhiyun pm_power_off = mcu_power_off;
171*4882a593Smuzhiyun dev_info(&client->dev, "will provide power-off service\n");
172*4882a593Smuzhiyun }
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun if (device_create_file(&client->dev, &dev_attr_status))
175*4882a593Smuzhiyun dev_err(&client->dev,
176*4882a593Smuzhiyun "couldn't create device file for status\n");
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun shutdown_thread = kthread_run(shutdown_thread_fn, NULL,
179*4882a593Smuzhiyun "mcu-i2c-shdn");
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun return 0;
182*4882a593Smuzhiyun err:
183*4882a593Smuzhiyun kfree(mcu);
184*4882a593Smuzhiyun return ret;
185*4882a593Smuzhiyun }
186*4882a593Smuzhiyun
mcu_remove(struct i2c_client * client)187*4882a593Smuzhiyun static int mcu_remove(struct i2c_client *client)
188*4882a593Smuzhiyun {
189*4882a593Smuzhiyun struct mcu *mcu = i2c_get_clientdata(client);
190*4882a593Smuzhiyun int ret;
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun kthread_stop(shutdown_thread);
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun device_remove_file(&client->dev, &dev_attr_status);
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun if (glob_mcu == mcu) {
197*4882a593Smuzhiyun pm_power_off = NULL;
198*4882a593Smuzhiyun glob_mcu = NULL;
199*4882a593Smuzhiyun }
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun ret = mcu_gpiochip_remove(mcu);
202*4882a593Smuzhiyun if (ret)
203*4882a593Smuzhiyun return ret;
204*4882a593Smuzhiyun kfree(mcu);
205*4882a593Smuzhiyun return 0;
206*4882a593Smuzhiyun }
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun static const struct i2c_device_id mcu_ids[] = {
209*4882a593Smuzhiyun { "mcu-mpc8349emitx", },
210*4882a593Smuzhiyun {},
211*4882a593Smuzhiyun };
212*4882a593Smuzhiyun MODULE_DEVICE_TABLE(i2c, mcu_ids);
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun static const struct of_device_id mcu_of_match_table[] = {
215*4882a593Smuzhiyun { .compatible = "fsl,mcu-mpc8349emitx", },
216*4882a593Smuzhiyun { },
217*4882a593Smuzhiyun };
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun static struct i2c_driver mcu_driver = {
220*4882a593Smuzhiyun .driver = {
221*4882a593Smuzhiyun .name = "mcu-mpc8349emitx",
222*4882a593Smuzhiyun .of_match_table = mcu_of_match_table,
223*4882a593Smuzhiyun },
224*4882a593Smuzhiyun .probe_new = mcu_probe,
225*4882a593Smuzhiyun .remove = mcu_remove,
226*4882a593Smuzhiyun .id_table = mcu_ids,
227*4882a593Smuzhiyun };
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun module_i2c_driver(mcu_driver);
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun MODULE_DESCRIPTION("Power Management and GPIO expander driver for "
232*4882a593Smuzhiyun "MPC8349E-mITX-compatible MCU");
233*4882a593Smuzhiyun MODULE_AUTHOR("Anton Vorontsov <avorontsov@ru.mvista.com>");
234*4882a593Smuzhiyun MODULE_LICENSE("GPL");
235