1 #include <linux/module.h>
2 #include <linux/platform_device.h>
3 #include <linux/mod_devicetable.h>
5 #include <linux/of_platform.h>
6 #include <asm-powerpc/mpc52xx.h>
7 #include <linux/interrupt.h>
10 struct mpc52xx_gpt *pwmf, *pwmb, *irca, *ircb;
17 static ssize_t show_position(struct device *dev,
18 struct device_attribute *attr, char *buf)
20 struct platform_device *pdev = to_platform_device(dev);
21 struct motorek *m = platform_get_drvdata(pdev);
23 int len = snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&m->pos));
24 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
27 static ssize_t show_action(struct device *dev,
28 struct device_attribute *attr, char *buf)
30 struct platform_device *pdev = to_platform_device(dev);
31 struct motorek *m = platform_get_drvdata(pdev);
33 int len = snprintf(buf, PAGE_SIZE, "%d\n", m->action);
34 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
37 static void motorek_action(struct motorek *m, int action_permile);
38 static ssize_t store_action(struct device *dev, struct device_attribute *attr,
39 const char *buf, size_t count)
41 struct platform_device *pdev = to_platform_device(dev);
42 struct motorek *m = platform_get_drvdata(pdev);
45 sscanf(buf, "%d", &a);
47 return strnlen(buf, PAGE_SIZE);
50 DEVICE_ATTR(position,0444,show_position,NULL);
51 DEVICE_ATTR(action,0644,show_action,store_action);
53 #define MPC52xx_GPT_MODE_DISABLED 0
54 #define MPC52xx_GPT_MODE_INCAPT 1
55 #define MPC52xx_GPT_MODE_OUTCMP 2
56 #define MPC52xx_GPT_MODE_PWM 3
57 #define MPC52xx_GPT_MODE_GPIO 4
59 #define MPC52xx_GPT_MODE_GPIO_IN (0<<4)
60 #define MPC52xx_GPT_MODE_GPIO_OUT0 (2<<4)
61 #define MPC52xx_GPT_MODE_GPIO_OUT1 (1<<4)
63 #define MPC52xx_GPT_STATUS_PIN (1<<8)
65 #define MPC52xx_GPT_PWM_OP (1<<8) /* PWM polarity */
68 * (/proc/device-tree/cpus/PowerPC,5200@0/bus-frequency) */
69 #define PWM_PERIOD 6600 /* = 132 000 kHz / 20 kHz */
72 static void pwm_width(struct mpc52xx_gpt *gpt, u16 width)
74 out_be32(&gpt->pwm, (width<<16) | MPC52xx_GPT_PWM_OP);
75 //printk("pwm: %p=0x%x\n", gpt, width);
79 static void __devinit init_pwn(struct mpc52xx_gpt *gpt)
81 //while (((unsigned)gpt & 0xff) != 0x50) gpt++;
82 out_be32(&gpt->count, (1<<16) | PWM_PERIOD);
84 out_be32(&gpt->mode, MPC52xx_GPT_MODE_PWM);
86 //out_be32(&gpt->pwm, (500<<16) | MPC52xx_GPT_PWM_OP); /* REMOVE ME */
88 //out_be32(&gpt->mode, MPC52xx_GPT_MODE_GPIO | (2<<4));
89 //printk("pwm: %p\n", gpt);
92 static void __devinit init_input(struct mpc52xx_gpt *gpt)
95 MPC52xx_GPT_MODE_GPIO |
96 MPC52xx_GPT_MODE_GPIO_IN);
99 static void pwm_done(struct mpc52xx_gpt *gpt)
101 out_be32(&gpt->mode, 0);
102 out_be32(&gpt->count, 0);
106 static void motorek_action(struct motorek *m, int action_permile)
108 m->action = action_permile;
109 if (action_permile >= 0) {
110 pwm_width(m->pwmb, 0);
111 pwm_width(m->pwmf, +action_permile*PWM_PERIOD/1000);
113 pwm_width(m->pwmf, 0);
114 pwm_width(m->pwmb, -action_permile*PWM_PERIOD/1000);
118 static int __devinit motorek_init(struct motorek *m)
127 static int motorek_done(struct motorek *m)
133 #define TRANSITION(old, new) (((old) << 2) | new)
134 static const int table[16] = {
135 [TRANSITION(0,1)] = +1,
136 [TRANSITION(1,3)] = +1,
137 [TRANSITION(3,2)] = +1,
138 [TRANSITION(2,0)] = +1,
140 [TRANSITION(1,0)] = -1,
141 [TRANSITION(3,1)] = -1,
142 [TRANSITION(2,3)] = -1,
143 [TRANSITION(0,2)] = -1,
146 static irqreturn_t motorek_irq(int irq, void *dev_id)
148 struct motorek *m = dev_id;
150 ((in_be32(&m->irca->status) & MPC52xx_GPT_STATUS_PIN) ? 1 : 0) | \
151 ((in_be32(&m->ircb->status) & MPC52xx_GPT_STATUS_PIN) ? 2 : 0);
152 atomic_add(table[TRANSITION(m->last_irc_state, irc_state)], &m->pos);
153 m->last_irc_state = irc_state;
157 static struct of_device_id mpc5200_gpt_match[] = {
158 { .compatible = "mpc5200-gpt", },
159 { .compatible = "fsl,mpc5200-gpt", },
163 struct mpc52xx_gpt __iomem *iomap_gpt_by_phandle_prop(struct device_node *dn, const char *prop)
165 struct device_node *np;
166 struct mpc52xx_gpt __iomem *gpt = NULL;
167 const phandle *phandle;
169 phandle = of_get_property(dn, prop, NULL);
173 np = of_find_node_by_phandle(*phandle);
177 if (!of_match_node(mpc5200_gpt_match, np)) {
178 printk(KERN_ERR "property %s doesn't refer to GPT\n", prop);
182 gpt = of_iomap(np, 0);
188 static int __devinit motorek_probe(struct of_device* dev,
189 const struct of_device_id *match)
191 struct device_node *dn = dev->node;
195 m = kzalloc(sizeof(*m), GFP_KERNEL);
201 m->pwmf = iomap_gpt_by_phandle_prop(dn, "pwmf");
204 m->pwmb = iomap_gpt_by_phandle_prop(dn, "pwmb");
207 m->irca = iomap_gpt_by_phandle_prop(dn, "irca");
210 m->ircb = iomap_gpt_by_phandle_prop(dn, "ircb");
214 m->irq = irq_of_parse_and_map(dn, 0);
215 if (m->irq == NO_IRQ)
217 err = request_irq(m->irq, motorek_irq, 0, "motorek", m);
223 platform_set_drvdata(dev, m);
224 err = device_create_file(&dev->dev,&dev_attr_position);
227 err = device_create_file(&dev->dev,&dev_attr_action);
231 printk(KERN_NOTICE "Motorek initialized\n");
250 static int __devexit motorek_remove(struct of_device* dev)
254 printk(KERN_NOTICE "Removing motorek\n");
255 m = platform_get_drvdata(dev);
262 static struct of_device_id motorek_match[] = {
263 { .type = "motorek", },
266 static struct of_platform_driver motorek_driver = {
267 .owner = THIS_MODULE,
269 .match_table = motorek_match,
270 .probe = motorek_probe,
271 .remove = __devexit_p(motorek_remove),
275 static int __init motorek_init_module(void)
278 struct of_device *dev;
279 struct device_node *dn;
281 for_each_node_by_type(dn, "motorek") {
282 if (!of_find_device_by_node(dn)) {
283 dev = of_platform_device_create(dn, NULL, NULL);
289 ret = of_register_platform_driver(&motorek_driver);
293 static void __exit motorek_exit_module(void)
295 struct of_device *dev;
296 struct device_node *dn;
298 for_each_node_by_type(dn, "motorek") {
299 while ((dev = of_find_device_by_node(dn))) {
300 of_device_unregister(dev);
303 of_unregister_platform_driver(&motorek_driver);
306 module_init(motorek_init_module);
307 module_exit(motorek_exit_module);
310 MODULE_LICENSE("GPL v2");
311 MODULE_VERSION("0.1");
312 MODULE_AUTHOR("Michal Sojka <sojkam1@fel.cvut.cz>");