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>
24 #include <asm/machdep.h>
27 * I don't have specifications for the MCU firmware, I found this register
28 * and bits positions by the trial&error method.
30 #define MCU_REG_CTRL 0x20
31 #define MCU_CTRL_POFF 0x40
33 #define MCU_NUM_GPIO 2
37 struct device_node
*np
;
38 struct i2c_client
*client
;
39 struct of_gpio_chip of_gc
;
43 static struct mcu
*glob_mcu
;
45 static void mcu_power_off(void)
47 struct mcu
*mcu
= glob_mcu
;
49 pr_info("Sending power-off request to the MCU...\n");
50 mutex_lock(&mcu
->lock
);
51 i2c_smbus_write_byte_data(glob_mcu
->client
, MCU_REG_CTRL
,
52 mcu
->reg_ctrl
| MCU_CTRL_POFF
);
53 mutex_unlock(&mcu
->lock
);
56 static void mcu_gpio_set(struct gpio_chip
*gc
, unsigned int gpio
, int val
)
58 struct of_gpio_chip
*of_gc
= to_of_gpio_chip(gc
);
59 struct mcu
*mcu
= container_of(of_gc
, struct mcu
, of_gc
);
60 u8 bit
= 1 << (4 + gpio
);
62 mutex_lock(&mcu
->lock
);
64 mcu
->reg_ctrl
&= ~bit
;
68 i2c_smbus_write_byte_data(mcu
->client
, MCU_REG_CTRL
, mcu
->reg_ctrl
);
69 mutex_unlock(&mcu
->lock
);
72 static int mcu_gpio_dir_out(struct gpio_chip
*gc
, unsigned int gpio
, int val
)
74 mcu_gpio_set(gc
, gpio
, val
);
78 static int mcu_gpiochip_add(struct mcu
*mcu
)
80 struct device_node
*np
;
81 struct of_gpio_chip
*of_gc
= &mcu
->of_gc
;
82 struct gpio_chip
*gc
= &of_gc
->gc
;
85 np
= of_find_compatible_node(NULL
, NULL
, "fsl,mcu-mpc8349emitx");
89 gc
->owner
= THIS_MODULE
;
90 gc
->label
= np
->full_name
;
92 gc
->ngpio
= MCU_NUM_GPIO
;
94 gc
->set
= mcu_gpio_set
;
95 gc
->direction_output
= mcu_gpio_dir_out
;
96 of_gc
->gpio_cells
= 2;
97 of_gc
->xlate
= of_gpio_simple_xlate
;
103 * We don't want to lose the node, its ->data and ->full_name...
104 * So, if succeeded, we don't put the node here.
106 ret
= gpiochip_add(gc
);
112 static int mcu_gpiochip_remove(struct mcu
*mcu
)
116 ret
= gpiochip_remove(&mcu
->of_gc
.gc
);
119 of_node_put(mcu
->np
);
124 static int __devinit
mcu_probe(struct i2c_client
*client
,
125 const struct i2c_device_id
*id
)
130 mcu
= kzalloc(sizeof(*mcu
), GFP_KERNEL
);
134 mutex_init(&mcu
->lock
);
135 mcu
->client
= client
;
136 i2c_set_clientdata(client
, mcu
);
138 ret
= i2c_smbus_read_byte_data(mcu
->client
, MCU_REG_CTRL
);
143 ret
= mcu_gpiochip_add(mcu
);
147 /* XXX: this is potentially racy, but there is no lock for ppc_md */
148 if (!ppc_md
.power_off
) {
150 ppc_md
.power_off
= mcu_power_off
;
151 dev_info(&client
->dev
, "will provide power-off service\n");
160 static int __devexit
mcu_remove(struct i2c_client
*client
)
162 struct mcu
*mcu
= i2c_get_clientdata(client
);
165 if (glob_mcu
== mcu
) {
166 ppc_md
.power_off
= NULL
;
170 ret
= mcu_gpiochip_remove(mcu
);
173 i2c_set_clientdata(client
, NULL
);
178 static const struct i2c_device_id mcu_ids
[] = {
179 { "mcu-mpc8349emitx", },
182 MODULE_DEVICE_TABLE(i2c
, mcu_ids
);
184 static struct i2c_driver mcu_driver
= {
186 .name
= "mcu-mpc8349emitx",
187 .owner
= THIS_MODULE
,
190 .remove
= __devexit_p(mcu_remove
),
194 static int __init
mcu_init(void)
196 return i2c_add_driver(&mcu_driver
);
198 module_init(mcu_init
);
200 static void __exit
mcu_exit(void)
202 i2c_del_driver(&mcu_driver
);
204 module_exit(mcu_exit
);
206 MODULE_DESCRIPTION("Power Management and GPIO expander driver for "
207 "MPC8349E-mITX-compatible MCU");
208 MODULE_AUTHOR("Anton Vorontsov <avorontsov@ru.mvista.com>");
209 MODULE_LICENSE("GPL");