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/init.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/device.h>
18 #include <linux/mutex.h>
19 #include <linux/i2c.h>
20 #include <linux/gpio.h>
22 #include <linux/of_gpio.h>
23 #include <linux/slab.h>
24 #include <linux/kthread.h>
25 #include <linux/reboot.h>
27 #include <asm/machdep.h>
30 * I don't have specifications for the MCU firmware, I found this register
31 * and bits positions by the trial&error method.
33 #define MCU_REG_CTRL 0x20
34 #define MCU_CTRL_POFF 0x40
35 #define MCU_CTRL_BTN 0x80
37 #define MCU_NUM_GPIO 2
41 struct i2c_client
*client
;
46 static struct mcu
*glob_mcu
;
48 struct task_struct
*shutdown_thread
;
49 static int shutdown_thread_fn(void *data
)
52 struct mcu
*mcu
= glob_mcu
;
54 while (!kthread_should_stop()) {
55 ret
= i2c_smbus_read_byte_data(mcu
->client
, MCU_REG_CTRL
);
57 pr_err("MCU status reg read failed.\n");
61 if (mcu
->reg_ctrl
& MCU_CTRL_BTN
) {
62 i2c_smbus_write_byte_data(mcu
->client
, MCU_REG_CTRL
,
63 mcu
->reg_ctrl
& ~MCU_CTRL_BTN
);
68 set_current_state(TASK_INTERRUPTIBLE
);
75 static ssize_t
show_status(struct device
*d
,
76 struct device_attribute
*attr
, char *buf
)
79 struct mcu
*mcu
= glob_mcu
;
81 ret
= i2c_smbus_read_byte_data(mcu
->client
, MCU_REG_CTRL
);
86 return sprintf(buf
, "%02x\n", ret
);
88 static DEVICE_ATTR(status
, S_IRUGO
, show_status
, NULL
);
90 static void mcu_power_off(void)
92 struct mcu
*mcu
= glob_mcu
;
94 pr_info("Sending power-off request to the MCU...\n");
95 mutex_lock(&mcu
->lock
);
96 i2c_smbus_write_byte_data(mcu
->client
, MCU_REG_CTRL
,
97 mcu
->reg_ctrl
| MCU_CTRL_POFF
);
98 mutex_unlock(&mcu
->lock
);
101 static void mcu_gpio_set(struct gpio_chip
*gc
, unsigned int gpio
, int val
)
103 struct mcu
*mcu
= container_of(gc
, struct mcu
, gc
);
104 u8 bit
= 1 << (4 + gpio
);
106 mutex_lock(&mcu
->lock
);
108 mcu
->reg_ctrl
&= ~bit
;
110 mcu
->reg_ctrl
|= bit
;
112 i2c_smbus_write_byte_data(mcu
->client
, MCU_REG_CTRL
, mcu
->reg_ctrl
);
113 mutex_unlock(&mcu
->lock
);
116 static int mcu_gpio_dir_out(struct gpio_chip
*gc
, unsigned int gpio
, int val
)
118 mcu_gpio_set(gc
, gpio
, val
);
122 static int mcu_gpiochip_add(struct mcu
*mcu
)
124 struct device_node
*np
;
125 struct gpio_chip
*gc
= &mcu
->gc
;
127 np
= of_find_compatible_node(NULL
, NULL
, "fsl,mcu-mpc8349emitx");
131 gc
->owner
= THIS_MODULE
;
132 gc
->label
= np
->full_name
;
134 gc
->ngpio
= MCU_NUM_GPIO
;
136 gc
->set
= mcu_gpio_set
;
137 gc
->direction_output
= mcu_gpio_dir_out
;
140 return gpiochip_add(gc
);
143 static int mcu_gpiochip_remove(struct mcu
*mcu
)
145 return gpiochip_remove(&mcu
->gc
);
148 static int __devinit
mcu_probe(struct i2c_client
*client
,
149 const struct i2c_device_id
*id
)
154 mcu
= kzalloc(sizeof(*mcu
), GFP_KERNEL
);
158 mutex_init(&mcu
->lock
);
159 mcu
->client
= client
;
160 i2c_set_clientdata(client
, mcu
);
162 ret
= i2c_smbus_read_byte_data(mcu
->client
, MCU_REG_CTRL
);
167 ret
= mcu_gpiochip_add(mcu
);
171 /* XXX: this is potentially racy, but there is no lock for ppc_md */
172 if (!ppc_md
.power_off
) {
174 ppc_md
.power_off
= mcu_power_off
;
175 dev_info(&client
->dev
, "will provide power-off service\n");
178 if (device_create_file(&client
->dev
, &dev_attr_status
))
179 dev_err(&client
->dev
,
180 "couldn't create device file for status\n");
182 shutdown_thread
= kthread_run(shutdown_thread_fn
, NULL
,
191 static int __devexit
mcu_remove(struct i2c_client
*client
)
193 struct mcu
*mcu
= i2c_get_clientdata(client
);
196 kthread_stop(shutdown_thread
);
198 device_remove_file(&client
->dev
, &dev_attr_status
);
200 if (glob_mcu
== mcu
) {
201 ppc_md
.power_off
= NULL
;
205 ret
= mcu_gpiochip_remove(mcu
);
208 i2c_set_clientdata(client
, NULL
);
213 static const struct i2c_device_id mcu_ids
[] = {
214 { "mcu-mpc8349emitx", },
217 MODULE_DEVICE_TABLE(i2c
, mcu_ids
);
219 static struct of_device_id mcu_of_match_table
[] __devinitdata
= {
220 { .compatible
= "fsl,mcu-mpc8349emitx", },
224 static struct i2c_driver mcu_driver
= {
226 .name
= "mcu-mpc8349emitx",
227 .owner
= THIS_MODULE
,
228 .of_match_table
= mcu_of_match_table
,
231 .remove
= __devexit_p(mcu_remove
),
235 static int __init
mcu_init(void)
237 return i2c_add_driver(&mcu_driver
);
239 module_init(mcu_init
);
241 static void __exit
mcu_exit(void)
243 i2c_del_driver(&mcu_driver
);
245 module_exit(mcu_exit
);
247 MODULE_DESCRIPTION("Power Management and GPIO expander driver for "
248 "MPC8349E-mITX-compatible MCU");
249 MODULE_AUTHOR("Anton Vorontsov <avorontsov@ru.mvista.com>");
250 MODULE_LICENSE("GPL");