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