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.
7 * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/device.h>
13 #include <linux/mutex.h>
14 #include <linux/i2c.h>
15 #include <linux/gpio/driver.h>
17 #include <linux/of_gpio.h>
18 #include <linux/slab.h>
19 #include <linux/kthread.h>
20 #include <linux/reboot.h>
22 #include <asm/machdep.h>
25 * I don't have specifications for the MCU firmware, I found this register
26 * and bits positions by the trial&error method.
28 #define MCU_REG_CTRL 0x20
29 #define MCU_CTRL_POFF 0x40
30 #define MCU_CTRL_BTN 0x80
32 #define MCU_NUM_GPIO 2
36 struct i2c_client
*client
;
41 static struct mcu
*glob_mcu
;
43 struct task_struct
*shutdown_thread
;
44 static int shutdown_thread_fn(void *data
)
47 struct mcu
*mcu
= glob_mcu
;
49 while (!kthread_should_stop()) {
50 ret
= i2c_smbus_read_byte_data(mcu
->client
, MCU_REG_CTRL
);
52 pr_err("MCU status reg read failed.\n");
56 if (mcu
->reg_ctrl
& MCU_CTRL_BTN
) {
57 i2c_smbus_write_byte_data(mcu
->client
, MCU_REG_CTRL
,
58 mcu
->reg_ctrl
& ~MCU_CTRL_BTN
);
63 set_current_state(TASK_INTERRUPTIBLE
);
70 static ssize_t
show_status(struct device
*d
,
71 struct device_attribute
*attr
, char *buf
)
74 struct mcu
*mcu
= glob_mcu
;
76 ret
= i2c_smbus_read_byte_data(mcu
->client
, MCU_REG_CTRL
);
81 return sprintf(buf
, "%02x\n", ret
);
83 static DEVICE_ATTR(status
, 0444, show_status
, NULL
);
85 static void mcu_power_off(void)
87 struct mcu
*mcu
= glob_mcu
;
89 pr_info("Sending power-off request to the MCU...\n");
90 mutex_lock(&mcu
->lock
);
91 i2c_smbus_write_byte_data(mcu
->client
, MCU_REG_CTRL
,
92 mcu
->reg_ctrl
| MCU_CTRL_POFF
);
93 mutex_unlock(&mcu
->lock
);
96 static void mcu_gpio_set(struct gpio_chip
*gc
, unsigned int gpio
, int val
)
98 struct mcu
*mcu
= gpiochip_get_data(gc
);
99 u8 bit
= 1 << (4 + gpio
);
101 mutex_lock(&mcu
->lock
);
103 mcu
->reg_ctrl
&= ~bit
;
105 mcu
->reg_ctrl
|= bit
;
107 i2c_smbus_write_byte_data(mcu
->client
, MCU_REG_CTRL
, mcu
->reg_ctrl
);
108 mutex_unlock(&mcu
->lock
);
111 static int mcu_gpio_dir_out(struct gpio_chip
*gc
, unsigned int gpio
, int val
)
113 mcu_gpio_set(gc
, gpio
, val
);
117 static int mcu_gpiochip_add(struct mcu
*mcu
)
119 struct device_node
*np
;
120 struct gpio_chip
*gc
= &mcu
->gc
;
122 np
= of_find_compatible_node(NULL
, NULL
, "fsl,mcu-mpc8349emitx");
126 gc
->owner
= THIS_MODULE
;
127 gc
->label
= kasprintf(GFP_KERNEL
, "%pOF", np
);
129 gc
->ngpio
= MCU_NUM_GPIO
;
131 gc
->set
= mcu_gpio_set
;
132 gc
->direction_output
= mcu_gpio_dir_out
;
135 return gpiochip_add_data(gc
, mcu
);
138 static int mcu_gpiochip_remove(struct mcu
*mcu
)
140 kfree(mcu
->gc
.label
);
141 gpiochip_remove(&mcu
->gc
);
145 static int mcu_probe(struct i2c_client
*client
)
150 mcu
= kzalloc(sizeof(*mcu
), GFP_KERNEL
);
154 mutex_init(&mcu
->lock
);
155 mcu
->client
= client
;
156 i2c_set_clientdata(client
, mcu
);
158 ret
= i2c_smbus_read_byte_data(mcu
->client
, MCU_REG_CTRL
);
163 ret
= mcu_gpiochip_add(mcu
);
167 /* XXX: this is potentially racy, but there is no lock for pm_power_off */
170 pm_power_off
= mcu_power_off
;
171 dev_info(&client
->dev
, "will provide power-off service\n");
174 if (device_create_file(&client
->dev
, &dev_attr_status
))
175 dev_err(&client
->dev
,
176 "couldn't create device file for status\n");
178 shutdown_thread
= kthread_run(shutdown_thread_fn
, NULL
,
187 static int mcu_remove(struct i2c_client
*client
)
189 struct mcu
*mcu
= i2c_get_clientdata(client
);
192 kthread_stop(shutdown_thread
);
194 device_remove_file(&client
->dev
, &dev_attr_status
);
196 if (glob_mcu
== mcu
) {
201 ret
= mcu_gpiochip_remove(mcu
);
208 static const struct i2c_device_id mcu_ids
[] = {
209 { "mcu-mpc8349emitx", },
212 MODULE_DEVICE_TABLE(i2c
, mcu_ids
);
214 static const struct of_device_id mcu_of_match_table
[] = {
215 { .compatible
= "fsl,mcu-mpc8349emitx", },
219 static struct i2c_driver mcu_driver
= {
221 .name
= "mcu-mpc8349emitx",
222 .of_match_table
= mcu_of_match_table
,
224 .probe_new
= mcu_probe
,
225 .remove
= mcu_remove
,
229 module_i2c_driver(mcu_driver
);
231 MODULE_DESCRIPTION("Power Management and GPIO expander driver for "
232 "MPC8349E-mITX-compatible MCU");
233 MODULE_AUTHOR("Anton Vorontsov <avorontsov@ru.mvista.com>");
234 MODULE_LICENSE("GPL");