]> rtime.felk.cvut.cz Git - shark/motorek-5200.git/blob - motorek.c
Add load command to the boot script required for newer novaboot version.
[shark/motorek-5200.git] / motorek.c
1 /*
2  *      Driver for DC motor connected to MPC5200 based boards.
3  *
4  *      The motor is used for education at Czech Technical University
5  *      in Prague.
6  *
7  *      Authors:
8  *      Michal Sojka            <sojkam1@fel.cvut.cz>
9  *
10  *      This program is free software; you can redistribute it and/or
11  *      modify it under the terms of the GNU General Public License
12  *      as published by the Free Software Foundation; either version
13  *      2 of the License, or (at your option) any later version.
14  */
15
16 #include <linux/module.h>
17 #include <linux/platform_device.h>
18 #include <linux/mod_devicetable.h>
19 #include <linux/of.h>
20 #include <linux/of_platform.h>
21 #include <asm/mpc52xx.h>
22 #include <linux/interrupt.h>
23 #include <linux/slab.h>
24
25 struct motorek {
26         struct mpc52xx_gpt *pwmf, *pwmb, *irca, *ircb;
27         atomic_t pos;
28         int irq;
29         int action;
30         int last_irc_state;
31 };
32
33 static ssize_t show_position(struct device *dev,
34                             struct device_attribute *attr, char *buf)
35 {
36         struct platform_device  *pdev = to_platform_device(dev);
37         struct motorek          *m = platform_get_drvdata(pdev);
38
39         int len = snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&m->pos));
40         return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
41 }
42
43 static ssize_t show_action(struct device *dev,
44                            struct device_attribute *attr, char *buf)
45 {
46         struct platform_device  *pdev = to_platform_device(dev);
47         struct motorek          *m = platform_get_drvdata(pdev);
48
49         int len = snprintf(buf, PAGE_SIZE, "%d\n", m->action);
50         return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
51 }
52
53 static void motorek_action(struct motorek *m, int action_permile);
54 static ssize_t store_action(struct device *dev, struct device_attribute *attr,
55                             const char *buf, size_t count)
56 {
57         struct platform_device  *pdev = to_platform_device(dev);
58         struct motorek          *m = platform_get_drvdata(pdev);
59
60         int a;
61         sscanf(buf, "%d", &a);
62         motorek_action(m, a);
63         return strnlen(buf, PAGE_SIZE);
64 }
65
66 DEVICE_ATTR(position,0444,show_position,NULL);
67 DEVICE_ATTR(action,0644,show_action,store_action);
68
69 #define MPC52xx_GPT_MODE_DISABLED 0
70 #define MPC52xx_GPT_MODE_INCAPT   1
71 #define MPC52xx_GPT_MODE_OUTCMP   2
72 #define MPC52xx_GPT_MODE_PWM      3
73 #define MPC52xx_GPT_MODE_GPIO     4
74
75 #define MPC52xx_GPT_MODE_GPIO_IN   (0<<4)
76 #define MPC52xx_GPT_MODE_GPIO_OUT0 (2<<4)
77 #define MPC52xx_GPT_MODE_GPIO_OUT1 (1<<4)
78
79 #define MPC52xx_GPT_STATUS_PIN (1<<8)
80
81 #define MPC52xx_GPT_PWM_OP        (1<<8) /* PWM polarity */
82
83 /* Base clock 132 MHz
84  * (/proc/device-tree/cpus/PowerPC,5200@0/bus-frequency) */
85 #define PWM_PERIOD 6600         /* = 132 000 kHz / 20 kHz  */
86
87
88 static void pwm_width(struct mpc52xx_gpt *gpt, u16 width)
89 {
90         out_be32(&gpt->pwm, (width<<16) | MPC52xx_GPT_PWM_OP);
91         //printk("pwm: %p=0x%x\n", gpt, width);
92
93 }
94
95 static void __devinit init_pwn(struct mpc52xx_gpt *gpt)
96 {
97         //while (((unsigned)gpt & 0xff) != 0x50) gpt++;
98         out_be32(&gpt->count, (1<<16) | PWM_PERIOD);
99         pwm_width(gpt, 0);
100         out_be32(&gpt->mode, MPC52xx_GPT_MODE_PWM);
101
102         //out_be32(&gpt->pwm, (500<<16) | MPC52xx_GPT_PWM_OP); /* REMOVE ME */
103
104         //out_be32(&gpt->mode, MPC52xx_GPT_MODE_GPIO | (2<<4));
105         //printk("pwm: %p\n", gpt);
106 }
107
108 static void __devinit init_input(struct mpc52xx_gpt *gpt)
109 {
110         out_be32(&gpt->mode,
111                  MPC52xx_GPT_MODE_GPIO |
112                  MPC52xx_GPT_MODE_GPIO_IN);
113 }
114
115 static void  pwm_done(struct mpc52xx_gpt *gpt)
116 {
117         out_be32(&gpt->mode, 0);
118         out_be32(&gpt->count, 0);
119         pwm_width(gpt, 0);
120 }
121
122 static void motorek_action(struct motorek *m, int action_permile)
123 {
124         m->action = action_permile;
125         if (action_permile >= 0) {
126                 pwm_width(m->pwmb, 0);
127                 pwm_width(m->pwmf, +action_permile*PWM_PERIOD/1000);
128         } else {
129                 pwm_width(m->pwmf, 0);
130                 pwm_width(m->pwmb, -action_permile*PWM_PERIOD/1000);
131         }
132 }
133
134 static int __devinit motorek_init(struct motorek *m)
135 {
136         init_pwn(m->pwmf);
137         init_pwn(m->pwmb);
138         init_input(m->irca);
139         init_input(m->ircb);
140         return 0;
141 }
142
143 static int motorek_done(struct motorek *m)
144 {
145         pwm_done(m->pwmf);
146         pwm_done(m->pwmb);
147         return 0;
148 }
149 #define TRANSITION(old, new) (((old) << 2) | new)
150 static const int table[16] = {
151         [TRANSITION(0,1)] = +1,
152         [TRANSITION(1,3)] = +1,
153         [TRANSITION(3,2)] = +1,
154         [TRANSITION(2,0)] = +1,
155
156         [TRANSITION(1,0)] = -1,
157         [TRANSITION(3,1)] = -1,
158         [TRANSITION(2,3)] = -1,
159         [TRANSITION(0,2)] = -1,
160 };
161
162 static irqreturn_t motorek_irq(int irq, void *dev_id)
163 {
164         struct motorek *m = dev_id;
165         int irc_state =
166                 ((in_be32(&m->irca->status) & MPC52xx_GPT_STATUS_PIN) ? 1 : 0) | \
167                 ((in_be32(&m->ircb->status) & MPC52xx_GPT_STATUS_PIN) ? 2 : 0);
168         atomic_add(table[TRANSITION(m->last_irc_state, irc_state)], &m->pos);
169         m->last_irc_state = irc_state;
170         return IRQ_HANDLED;
171 }
172
173 static struct of_device_id mpc5200_gpt_match[] = {
174         { .compatible = "mpc5200-gpt", },
175         { .compatible = "fsl,mpc5200-gpt", },
176         {},
177 };
178
179 struct mpc52xx_gpt __iomem *iomap_gpt_by_phandle_prop(struct device_node *dn, const char *prop)
180 {
181         struct device_node *np;
182         struct mpc52xx_gpt __iomem *gpt = NULL;
183         const phandle *phandle;
184
185         phandle = of_get_property(dn, prop, NULL);
186         if (!phandle)
187                 return NULL;
188
189         np = of_find_node_by_phandle(*phandle);
190         if (!np)
191                 return NULL;
192
193         if (!of_match_node(mpc5200_gpt_match, np)) {
194                 printk(KERN_ERR "property %s doesn't refer to GPT\n", prop);
195                 goto out;
196         }
197
198         gpt = of_iomap(np, 0);
199 out:
200         of_node_put(np);
201         return gpt;
202 }
203
204 static int __devinit motorek_probe(struct platform_device* dev)
205 {
206         struct device_node *dn = dev->dev.of_node;
207         struct motorek *m;
208         int err;
209
210         m = kzalloc(sizeof(*m), GFP_KERNEL);
211         if (!m)
212                 return -ENOMEM;
213
214         err = -ENODEV;
215
216         m->pwmf = iomap_gpt_by_phandle_prop(dn, "pwmf");
217         if (!m->pwmf)
218                 goto err_free;
219         m->pwmb = iomap_gpt_by_phandle_prop(dn, "pwmb");
220         if (!m->pwmb)
221                 goto err_unmap_pf;
222         m->irca = iomap_gpt_by_phandle_prop(dn, "irca");
223         if (!m->irca)
224                 goto err_unmap_pb;
225         m->ircb = iomap_gpt_by_phandle_prop(dn, "ircb");
226         if (!m->ircb)
227                 goto err_unmap_ia;
228
229         m->irq = irq_of_parse_and_map(dn, 0);
230         if (m->irq == NO_IRQ)
231                 goto err_unmap_ib;
232         err = request_irq(m->irq, motorek_irq, 0, "motorek", m);
233         if (err)
234                 goto err_unmap_ib;
235
236         motorek_init(m);
237
238         platform_set_drvdata(dev, m);
239         err = device_create_file(&dev->dev,&dev_attr_position);
240         if (err)
241                 goto err_irq;
242         err = device_create_file(&dev->dev,&dev_attr_action);
243         if (err)
244                 goto err_irq;
245
246         printk(KERN_NOTICE "Motorek initialized\n");
247
248         return 0;
249
250 err_irq:
251         free_irq(m->irq, m);
252 err_unmap_ib:
253         iounmap(m->pwmb);
254 err_unmap_ia:
255         iounmap(m->pwmf);
256 err_unmap_pb:
257         iounmap(m->pwmb);
258 err_unmap_pf:
259         iounmap(m->pwmf);
260 err_free:
261         kfree(m);
262         return err;
263 }
264
265 static int __devexit motorek_remove(struct platform_device* dev)
266 {
267         struct motorek *m;
268
269         printk(KERN_NOTICE "Removing motorek\n");
270         m = platform_get_drvdata(dev);
271         free_irq(m->irq, m);
272         motorek_done(m);
273         kfree(m);
274         return 0;
275 }
276
277 static struct of_device_id motorek_match[] = {
278         { .type = "motorek", },
279         {},
280 };
281 static struct platform_driver motorek_driver = {
282         .probe          = motorek_probe,
283         .remove         = __devexit_p(motorek_remove),
284         .driver         = {
285                 .name           = "motorek",
286                 .owner          = THIS_MODULE,
287                 .of_match_table = motorek_match,
288         },
289 };
290
291
292 static int __init motorek_init_module(void)
293 {
294         int ret;
295         struct platform_device *dev;
296         struct device_node *dn;
297
298         for_each_node_by_type(dn, "motorek") {
299                 if (!of_find_device_by_node(dn)) {
300                         dev = of_platform_device_create(dn, NULL, NULL);
301                         if (!dev)
302                                 return -ENOMEM;
303                 }
304         };
305
306         ret = platform_driver_register(&motorek_driver);
307         return ret;
308 }
309
310 static void __exit motorek_exit_module(void)
311 {
312         struct platform_device *dev;
313         struct device_node *dn;
314
315         for_each_node_by_type(dn, "motorek") {
316                 while ((dev = of_find_device_by_node(dn))) {
317                         of_device_unregister(dev);
318                 }
319         }
320         platform_driver_unregister(&motorek_driver);
321 }
322
323 module_init(motorek_init_module);
324 module_exit(motorek_exit_module);
325
326
327 MODULE_LICENSE("GPL v2");
328 MODULE_VERSION("0.1");
329 MODULE_AUTHOR("Michal Sojka <sojkam1@fel.cvut.cz>");