]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - arch/powerpc/platforms/83xx/mcu_mpc8349emitx.c
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[mirror_ubuntu-zesty-kernel.git] / arch / powerpc / platforms / 83xx / mcu_mpc8349emitx.c
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>
23 #include <linux/slab.h>
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;
40 struct of_gpio_chip of_gc;
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 {
59 struct of_gpio_chip *of_gc = to_of_gpio_chip(gc);
60 struct mcu *mcu = container_of(of_gc, struct mcu, of_gc);
61 u8 bit = 1 << (4 + gpio);
62
63 mutex_lock(&mcu->lock);
64 if (val)
65 mcu->reg_ctrl &= ~bit;
66 else
67 mcu->reg_ctrl |= bit;
68
69 i2c_smbus_write_byte_data(mcu->client, MCU_REG_CTRL, mcu->reg_ctrl);
70 mutex_unlock(&mcu->lock);
71 }
72
73 static int mcu_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
74 {
75 mcu_gpio_set(gc, gpio, val);
76 return 0;
77 }
78
79 static int mcu_gpiochip_add(struct mcu *mcu)
80 {
81 struct device_node *np;
82 struct of_gpio_chip *of_gc = &mcu->of_gc;
83 struct gpio_chip *gc = &of_gc->gc;
84 int ret;
85
86 np = of_find_compatible_node(NULL, NULL, "fsl,mcu-mpc8349emitx");
87 if (!np)
88 return -ENODEV;
89
90 gc->owner = THIS_MODULE;
91 gc->label = np->full_name;
92 gc->can_sleep = 1;
93 gc->ngpio = MCU_NUM_GPIO;
94 gc->base = -1;
95 gc->set = mcu_gpio_set;
96 gc->direction_output = mcu_gpio_dir_out;
97 of_gc->gpio_cells = 2;
98 of_gc->xlate = of_gpio_simple_xlate;
99
100 np->data = of_gc;
101 mcu->np = np;
102
103 /*
104 * We don't want to lose the node, its ->data and ->full_name...
105 * So, if succeeded, we don't put the node here.
106 */
107 ret = gpiochip_add(gc);
108 if (ret)
109 of_node_put(np);
110 return ret;
111 }
112
113 static int mcu_gpiochip_remove(struct mcu *mcu)
114 {
115 int ret;
116
117 ret = gpiochip_remove(&mcu->of_gc.gc);
118 if (ret)
119 return ret;
120 of_node_put(mcu->np);
121
122 return 0;
123 }
124
125 static int __devinit mcu_probe(struct i2c_client *client,
126 const struct i2c_device_id *id)
127 {
128 struct mcu *mcu;
129 int ret;
130
131 mcu = kzalloc(sizeof(*mcu), GFP_KERNEL);
132 if (!mcu)
133 return -ENOMEM;
134
135 mutex_init(&mcu->lock);
136 mcu->client = client;
137 i2c_set_clientdata(client, mcu);
138
139 ret = i2c_smbus_read_byte_data(mcu->client, MCU_REG_CTRL);
140 if (ret < 0)
141 goto err;
142 mcu->reg_ctrl = ret;
143
144 ret = mcu_gpiochip_add(mcu);
145 if (ret)
146 goto err;
147
148 /* XXX: this is potentially racy, but there is no lock for ppc_md */
149 if (!ppc_md.power_off) {
150 glob_mcu = mcu;
151 ppc_md.power_off = mcu_power_off;
152 dev_info(&client->dev, "will provide power-off service\n");
153 }
154
155 return 0;
156 err:
157 kfree(mcu);
158 return ret;
159 }
160
161 static int __devexit mcu_remove(struct i2c_client *client)
162 {
163 struct mcu *mcu = i2c_get_clientdata(client);
164 int ret;
165
166 if (glob_mcu == mcu) {
167 ppc_md.power_off = NULL;
168 glob_mcu = NULL;
169 }
170
171 ret = mcu_gpiochip_remove(mcu);
172 if (ret)
173 return ret;
174 i2c_set_clientdata(client, NULL);
175 kfree(mcu);
176 return 0;
177 }
178
179 static const struct i2c_device_id mcu_ids[] = {
180 { "mcu-mpc8349emitx", },
181 {},
182 };
183 MODULE_DEVICE_TABLE(i2c, mcu_ids);
184
185 static struct i2c_driver mcu_driver = {
186 .driver = {
187 .name = "mcu-mpc8349emitx",
188 .owner = THIS_MODULE,
189 },
190 .probe = mcu_probe,
191 .remove = __devexit_p(mcu_remove),
192 .id_table = mcu_ids,
193 };
194
195 static int __init mcu_init(void)
196 {
197 return i2c_add_driver(&mcu_driver);
198 }
199 module_init(mcu_init);
200
201 static void __exit mcu_exit(void)
202 {
203 i2c_del_driver(&mcu_driver);
204 }
205 module_exit(mcu_exit);
206
207 MODULE_DESCRIPTION("Power Management and GPIO expander driver for "
208 "MPC8349E-mITX-compatible MCU");
209 MODULE_AUTHOR("Anton Vorontsov <avorontsov@ru.mvista.com>");
210 MODULE_LICENSE("GPL");