]>
Commit | Line | Data |
---|---|---|
d6c3db83 AV |
1 | /* |
2 | * Power Management and GPIO expander driver for MPC8349E-mITX-compatible MCU | |
3 | * | |
4 | * Copyright (c) 2008 MontaVista Software, Inc. | |
5 | * | |
6 | * Author: Anton Vorontsov <avorontsov@ru.mvista.com> | |
7 | * | |
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. | |
12 | */ | |
13 | ||
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> | |
21 | #include <linux/of.h> | |
22 | #include <linux/of_gpio.h> | |
5a0e3ad6 | 23 | #include <linux/slab.h> |
d6c3db83 AV |
24 | #include <asm/prom.h> |
25 | #include <asm/machdep.h> | |
26 | ||
27 | /* | |
28 | * I don't have specifications for the MCU firmware, I found this register | |
29 | * and bits positions by the trial&error method. | |
30 | */ | |
31 | #define MCU_REG_CTRL 0x20 | |
32 | #define MCU_CTRL_POFF 0x40 | |
33 | ||
34 | #define MCU_NUM_GPIO 2 | |
35 | ||
36 | struct mcu { | |
37 | struct mutex lock; | |
38 | struct device_node *np; | |
39 | struct i2c_client *client; | |
a19e3da5 | 40 | struct gpio_chip gc; |
d6c3db83 AV |
41 | u8 reg_ctrl; |
42 | }; | |
43 | ||
44 | static struct mcu *glob_mcu; | |
45 | ||
46 | static void mcu_power_off(void) | |
47 | { | |
48 | struct mcu *mcu = glob_mcu; | |
49 | ||
50 | pr_info("Sending power-off request to the MCU...\n"); | |
51 | mutex_lock(&mcu->lock); | |
52 | i2c_smbus_write_byte_data(glob_mcu->client, MCU_REG_CTRL, | |
53 | mcu->reg_ctrl | MCU_CTRL_POFF); | |
54 | mutex_unlock(&mcu->lock); | |
55 | } | |
56 | ||
57 | static void mcu_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val) | |
58 | { | |
a19e3da5 | 59 | struct mcu *mcu = container_of(gc, struct mcu, gc); |
d6c3db83 AV |
60 | u8 bit = 1 << (4 + gpio); |
61 | ||
62 | mutex_lock(&mcu->lock); | |
63 | if (val) | |
64 | mcu->reg_ctrl &= ~bit; | |
65 | else | |
66 | mcu->reg_ctrl |= bit; | |
67 | ||
68 | i2c_smbus_write_byte_data(mcu->client, MCU_REG_CTRL, mcu->reg_ctrl); | |
69 | mutex_unlock(&mcu->lock); | |
70 | } | |
71 | ||
72 | static int mcu_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val) | |
73 | { | |
74 | mcu_gpio_set(gc, gpio, val); | |
75 | return 0; | |
76 | } | |
77 | ||
78 | static int mcu_gpiochip_add(struct mcu *mcu) | |
79 | { | |
80 | struct device_node *np; | |
a19e3da5 | 81 | struct gpio_chip *gc = &mcu->gc; |
d6c3db83 AV |
82 | int ret; |
83 | ||
84 | np = of_find_compatible_node(NULL, NULL, "fsl,mcu-mpc8349emitx"); | |
85 | if (!np) | |
86 | return -ENODEV; | |
87 | ||
88 | gc->owner = THIS_MODULE; | |
89 | gc->label = np->full_name; | |
90 | gc->can_sleep = 1; | |
91 | gc->ngpio = MCU_NUM_GPIO; | |
92 | gc->base = -1; | |
93 | gc->set = mcu_gpio_set; | |
94 | gc->direction_output = mcu_gpio_dir_out; | |
a19e3da5 AV |
95 | gc->of_gpio_n_cells = 2; |
96 | gc->of_xlate = of_gpio_simple_xlate; | |
d6c3db83 | 97 | |
d6c3db83 AV |
98 | mcu->np = np; |
99 | ||
100 | /* | |
101 | * We don't want to lose the node, its ->data and ->full_name... | |
102 | * So, if succeeded, we don't put the node here. | |
103 | */ | |
104 | ret = gpiochip_add(gc); | |
105 | if (ret) | |
106 | of_node_put(np); | |
107 | return ret; | |
108 | } | |
109 | ||
110 | static int mcu_gpiochip_remove(struct mcu *mcu) | |
111 | { | |
112 | int ret; | |
113 | ||
a19e3da5 | 114 | ret = gpiochip_remove(&mcu->gc); |
d6c3db83 AV |
115 | if (ret) |
116 | return ret; | |
117 | of_node_put(mcu->np); | |
118 | ||
119 | return 0; | |
120 | } | |
121 | ||
122 | static int __devinit mcu_probe(struct i2c_client *client, | |
123 | const struct i2c_device_id *id) | |
124 | { | |
125 | struct mcu *mcu; | |
126 | int ret; | |
127 | ||
128 | mcu = kzalloc(sizeof(*mcu), GFP_KERNEL); | |
129 | if (!mcu) | |
130 | return -ENOMEM; | |
131 | ||
132 | mutex_init(&mcu->lock); | |
133 | mcu->client = client; | |
134 | i2c_set_clientdata(client, mcu); | |
135 | ||
136 | ret = i2c_smbus_read_byte_data(mcu->client, MCU_REG_CTRL); | |
137 | if (ret < 0) | |
138 | goto err; | |
139 | mcu->reg_ctrl = ret; | |
140 | ||
141 | ret = mcu_gpiochip_add(mcu); | |
142 | if (ret) | |
143 | goto err; | |
144 | ||
145 | /* XXX: this is potentially racy, but there is no lock for ppc_md */ | |
146 | if (!ppc_md.power_off) { | |
147 | glob_mcu = mcu; | |
148 | ppc_md.power_off = mcu_power_off; | |
149 | dev_info(&client->dev, "will provide power-off service\n"); | |
150 | } | |
151 | ||
152 | return 0; | |
153 | err: | |
154 | kfree(mcu); | |
155 | return ret; | |
156 | } | |
157 | ||
158 | static int __devexit mcu_remove(struct i2c_client *client) | |
159 | { | |
160 | struct mcu *mcu = i2c_get_clientdata(client); | |
161 | int ret; | |
162 | ||
163 | if (glob_mcu == mcu) { | |
164 | ppc_md.power_off = NULL; | |
165 | glob_mcu = NULL; | |
166 | } | |
167 | ||
168 | ret = mcu_gpiochip_remove(mcu); | |
169 | if (ret) | |
170 | return ret; | |
171 | i2c_set_clientdata(client, NULL); | |
172 | kfree(mcu); | |
173 | return 0; | |
174 | } | |
175 | ||
176 | static const struct i2c_device_id mcu_ids[] = { | |
177 | { "mcu-mpc8349emitx", }, | |
178 | {}, | |
179 | }; | |
180 | MODULE_DEVICE_TABLE(i2c, mcu_ids); | |
181 | ||
182 | static struct i2c_driver mcu_driver = { | |
183 | .driver = { | |
184 | .name = "mcu-mpc8349emitx", | |
185 | .owner = THIS_MODULE, | |
186 | }, | |
187 | .probe = mcu_probe, | |
188 | .remove = __devexit_p(mcu_remove), | |
189 | .id_table = mcu_ids, | |
190 | }; | |
191 | ||
192 | static int __init mcu_init(void) | |
193 | { | |
194 | return i2c_add_driver(&mcu_driver); | |
195 | } | |
196 | module_init(mcu_init); | |
197 | ||
198 | static void __exit mcu_exit(void) | |
199 | { | |
200 | i2c_del_driver(&mcu_driver); | |
201 | } | |
202 | module_exit(mcu_exit); | |
203 | ||
204 | MODULE_DESCRIPTION("Power Management and GPIO expander driver for " | |
205 | "MPC8349E-mITX-compatible MCU"); | |
206 | MODULE_AUTHOR("Anton Vorontsov <avorontsov@ru.mvista.com>"); | |
207 | MODULE_LICENSE("GPL"); |