1 #include <linux/module.h>
2 #include <linux/platform_device.h>
3 #include <linux/mod_devicetable.h>
5 #include <asm-powerpc/mpc52xx.h>
6 #include <linux/interrupt.h>
9 struct mpc52xx_gpt *pwmf, *pwmb;
14 static ssize_t show_position(struct device *dev,
15 struct device_attribute *attr, char *buf)
17 struct platform_device *pdev = to_platform_device(dev);
18 struct motorek *m = platform_get_drvdata(pdev);
20 int len = snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&m->pos));
21 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
24 DEVICE_ATTR(position,0444,show_position,NULL);
26 #define MPC52xx_GPT_MODE_DISABLED 0
27 #define MPC52xx_GPT_MODE_INCAPT 1
28 #define MPC52xx_GPT_MODE_OUTCMP 2
29 #define MPC52xx_GPT_MODE_PWM 3
30 #define MPC52xx_GPT_MODE_GPIO 4
32 #define MPC52xx_GPT_PWM_OP (1<<8)
35 * (/proc/device-tree/cpus/PowerPC,5200@0/bus-frequency) */
36 #define PWM_PERIOD 6600 /* = 132 000 kHz / 20 kHz */
39 static void pwm_width(struct mpc52xx_gpt *gpt, u16 width)
41 out_be32(&gpt->pwm, (width<<16) | MPC52xx_GPT_PWM_OP);
44 static void __devinit pwm_init(struct mpc52xx_gpt *gpt)
46 out_be32(&gpt->count, (1<<16) | PWM_PERIOD);
48 out_be32(&gpt->mode, MPC52xx_GPT_MODE_PWM);
51 static void pwm_done(struct mpc52xx_gpt *gpt)
53 out_be32(&gpt->mode, 0);
54 out_be32(&gpt->count, 0);
58 static void motorek_action(struct motorek *m, int action_permile)
60 if (action_permile >= 0) {
61 pwm_width(m->pwmb, 0);
62 pwm_width(m->pwmf, +action_permile*PWM_PERIOD/1000);
64 pwm_width(m->pwmf, 0);
65 pwm_width(m->pwmb, -action_permile*PWM_PERIOD/1000);
69 static int __devinit motorek_init(struct motorek *m)
73 /* motorek_action(m, +45); */
74 pwm_width(m->pwmb, 500);
75 pwm_width(m->pwmf, 500);
79 static int motorek_done(struct motorek *m)
86 static irqreturn_t motorek_irq(int irq, void *dev_id)
88 struct motorek *m = dev_id;
93 /* mpc5200 device tree match tables */
94 static struct of_device_id mpc5200_gpt_ids[] __initdata = {
95 { .compatible = "fsl,mpc5200-gpt", },
96 { .compatible = "mpc5200-gpt", },
100 static int __devinit motorek_probe(struct platform_device *dev)
102 struct device_node *np;
106 m = kzalloc(sizeof(*m), GFP_KERNEL);
110 for_each_matching_node(np, mpc5200_gpt_ids) {
114 prop = of_get_property(np, "cell-index", NULL);
119 m->pwmf = of_iomap(np, 0);
122 m->pwmb = of_iomap(np, 0);
127 if (!m->pwmf || !m->pwmb) {
128 printk(KERN_ERR "%s() mmap failed\n", __func__);
132 /* /\* FIXME: This should be specified in device-tree *\/ */
133 /* m->irq = irq_create_of_mapping( */
135 /* err = request_irq(m->irq, motorek_irq, 0, "motorek", m); */
141 platform_set_drvdata(dev, m);
143 err = device_create_file(&dev->dev,&dev_attr_position);
147 printk(KERN_NOTICE "Motorek initialized\n");
152 static int __devexit motorek_remove(struct platform_device *dev)
155 m = platform_get_drvdata(dev);
157 printk(KERN_NOTICE "Removing motorek\n");
165 static struct platform_driver motorek_driver = {
166 .probe = motorek_probe,
167 .remove = __devexit_p(motorek_remove),
170 .owner = THIS_MODULE,
175 static struct platform_device *motorek_pdev;
177 static int __init motorek_init_module(void)
180 motorek_pdev = platform_device_alloc("motorek", 0);
183 ret = platform_device_add(motorek_pdev);
185 platform_device_put(motorek_pdev);
189 ret = platform_driver_register(&motorek_driver);
192 module_init(motorek_init_module);
194 static void __exit motorek_exit_module(void)
196 platform_device_unregister(motorek_pdev);
197 platform_driver_unregister(&motorek_driver);
199 module_exit(motorek_exit_module);
202 MODULE_LICENSE("GPL v2");
203 MODULE_VERSION("0.1");
204 MODULE_AUTHOR("Michal Sojka <sojkam1@fel.cvut.cz>");