2 * Driver for DC motor connected to MPC5200 based boards.
4 * The motor is used for education at Czech Technical University
8 * Michal Sojka <sojkam1@fel.cvut.cz>
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.
16 #include <linux/module.h>
17 #include <linux/platform_device.h>
18 #include <linux/mod_devicetable.h>
20 #include <linux/of_platform.h>
21 #include <asm/mpc52xx.h>
22 #include <linux/interrupt.h>
23 #include <linux/slab.h>
26 struct mpc52xx_gpt *pwmf, *pwmb, *irca, *ircb;
33 static ssize_t show_position(struct device *dev,
34 struct device_attribute *attr, char *buf)
36 struct platform_device *pdev = to_platform_device(dev);
37 struct motorek *m = platform_get_drvdata(pdev);
39 int len = snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&m->pos));
40 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
43 static ssize_t show_action(struct device *dev,
44 struct device_attribute *attr, char *buf)
46 struct platform_device *pdev = to_platform_device(dev);
47 struct motorek *m = platform_get_drvdata(pdev);
49 int len = snprintf(buf, PAGE_SIZE, "%d\n", m->action);
50 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
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)
57 struct platform_device *pdev = to_platform_device(dev);
58 struct motorek *m = platform_get_drvdata(pdev);
61 sscanf(buf, "%d", &a);
63 return strnlen(buf, PAGE_SIZE);
66 DEVICE_ATTR(position,0444,show_position,NULL);
67 DEVICE_ATTR(action,0644,show_action,store_action);
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
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)
79 #define MPC52xx_GPT_STATUS_PIN (1<<8)
81 #define MPC52xx_GPT_PWM_OP (1<<8) /* PWM polarity */
84 * (/proc/device-tree/cpus/PowerPC,5200@0/bus-frequency) */
85 #define PWM_PERIOD 6600 /* = 132 000 kHz / 20 kHz */
88 static void pwm_width(struct mpc52xx_gpt *gpt, u16 width)
90 out_be32(&gpt->pwm, (width<<16) | MPC52xx_GPT_PWM_OP);
91 //printk("pwm: %p=0x%x\n", gpt, width);
95 static void __devinit init_pwn(struct mpc52xx_gpt *gpt)
97 //while (((unsigned)gpt & 0xff) != 0x50) gpt++;
98 out_be32(&gpt->count, (1<<16) | PWM_PERIOD);
100 out_be32(&gpt->mode, MPC52xx_GPT_MODE_PWM);
102 //out_be32(&gpt->pwm, (500<<16) | MPC52xx_GPT_PWM_OP); /* REMOVE ME */
104 //out_be32(&gpt->mode, MPC52xx_GPT_MODE_GPIO | (2<<4));
105 //printk("pwm: %p\n", gpt);
108 static void __devinit init_input(struct mpc52xx_gpt *gpt)
111 MPC52xx_GPT_MODE_GPIO |
112 MPC52xx_GPT_MODE_GPIO_IN);
115 static void pwm_done(struct mpc52xx_gpt *gpt)
117 out_be32(&gpt->mode, 0);
118 out_be32(&gpt->count, 0);
122 static void motorek_action(struct motorek *m, int action_permile)
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);
129 pwm_width(m->pwmf, 0);
130 pwm_width(m->pwmb, -action_permile*PWM_PERIOD/1000);
134 static int __devinit motorek_init(struct motorek *m)
143 static int motorek_done(struct motorek *m)
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,
156 [TRANSITION(1,0)] = -1,
157 [TRANSITION(3,1)] = -1,
158 [TRANSITION(2,3)] = -1,
159 [TRANSITION(0,2)] = -1,
162 static irqreturn_t motorek_irq(int irq, void *dev_id)
164 struct motorek *m = dev_id;
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;
173 static struct of_device_id mpc5200_gpt_match[] = {
174 { .compatible = "mpc5200-gpt", },
175 { .compatible = "fsl,mpc5200-gpt", },
179 struct mpc52xx_gpt __iomem *iomap_gpt_by_phandle_prop(struct device_node *dn, const char *prop)
181 struct device_node *np;
182 struct mpc52xx_gpt __iomem *gpt = NULL;
183 const phandle *phandle;
185 phandle = of_get_property(dn, prop, NULL);
189 np = of_find_node_by_phandle(*phandle);
193 if (!of_match_node(mpc5200_gpt_match, np)) {
194 printk(KERN_ERR "property %s doesn't refer to GPT\n", prop);
198 gpt = of_iomap(np, 0);
204 static int __devinit motorek_probe(struct platform_device* dev)
206 struct device_node *dn = dev->dev.of_node;
210 m = kzalloc(sizeof(*m), GFP_KERNEL);
216 m->pwmf = iomap_gpt_by_phandle_prop(dn, "pwmf");
219 m->pwmb = iomap_gpt_by_phandle_prop(dn, "pwmb");
222 m->irca = iomap_gpt_by_phandle_prop(dn, "irca");
225 m->ircb = iomap_gpt_by_phandle_prop(dn, "ircb");
229 m->irq = irq_of_parse_and_map(dn, 0);
230 if (m->irq == NO_IRQ)
232 err = request_irq(m->irq, motorek_irq, 0, "motorek", m);
238 platform_set_drvdata(dev, m);
239 err = device_create_file(&dev->dev,&dev_attr_position);
242 err = device_create_file(&dev->dev,&dev_attr_action);
246 printk(KERN_NOTICE "Motorek initialized\n");
265 static int __devexit motorek_remove(struct platform_device* dev)
269 printk(KERN_NOTICE "Removing motorek\n");
270 m = platform_get_drvdata(dev);
277 static struct of_device_id motorek_match[] = {
278 { .type = "motorek", },
281 static struct platform_driver motorek_driver = {
282 .probe = motorek_probe,
283 .remove = __devexit_p(motorek_remove),
286 .owner = THIS_MODULE,
287 .of_match_table = motorek_match,
292 static int __init motorek_init_module(void)
295 struct platform_device *dev;
296 struct device_node *dn;
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);
306 ret = platform_driver_register(&motorek_driver);
310 static void __exit motorek_exit_module(void)
312 struct platform_device *dev;
313 struct device_node *dn;
315 for_each_node_by_type(dn, "motorek") {
316 while ((dev = of_find_device_by_node(dn))) {
317 of_device_unregister(dev);
320 platform_driver_unregister(&motorek_driver);
323 module_init(motorek_init_module);
324 module_exit(motorek_exit_module);
327 MODULE_LICENSE("GPL v2");
328 MODULE_VERSION("0.1");
329 MODULE_AUTHOR("Michal Sojka <sojkam1@fel.cvut.cz>");