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 gpt->pwm = (width<<16) | MPC52xx_GPT_PWM_OP;
44 static void __devinit pwm_init(struct mpc52xx_gpt *gpt)
46 gpt->count = (1<<16) | PWM_PERIOD;
48 gpt->mode = MPC52xx_GPT_MODE_PWM;
51 static void pwm_done(struct mpc52xx_gpt *gpt)
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);
77 static int motorek_done(struct motorek *m)
84 static irqreturn_t motorek_irq(int irq, void *dev_id)
86 struct motorek *m = dev_id;
91 /* mpc5200 device tree match tables */
92 static struct of_device_id mpc5200_gpt_ids[] __initdata = {
93 { .compatible = "fsl,mpc5200-gpt", },
94 { .compatible = "mpc5200-gpt", },
98 static int __devinit motorek_probe(struct platform_device *dev)
100 struct device_node *np;
104 m = kzalloc(sizeof(*m), GFP_KERNEL);
108 for_each_matching_node(np, mpc5200_gpt_ids) {
112 prop = of_get_property(np, "cell-index", NULL);
117 m->pwmf = of_iomap(np, 0);
120 m->pwmb = of_iomap(np, 0);
125 if (!m->pwmf || !m->pwmb) {
126 printk(KERN_ERR "%s() mmap failed\n", __func__);
130 /* /\* FIXME: This should be specified in device-tree *\/ */
131 /* m->irq = irq_create_of_mapping( */
133 /* err = request_irq(m->irq, motorek_irq, 0, "motorek", m); */
139 platform_set_drvdata(dev, m);
141 err = device_create_file(&dev->dev,&dev_attr_position);
148 static int __devexit motorek_remove(struct platform_device *dev)
151 m = platform_get_drvdata(dev);
159 static struct platform_driver motorek_driver = {
160 .probe = motorek_probe,
161 .remove = __devexit_p(motorek_remove),
164 .owner = THIS_MODULE,
169 static struct platform_device *motorek_pdev;
171 static int __init motorek_init_module(void)
174 motorek_pdev = platform_device_alloc("motorek", 0);
177 ret = platform_device_add(motorek_pdev);
179 platform_device_put(motorek_pdev);
183 ret = platform_driver_register(&motorek_driver);
186 module_init(motorek_init_module);
188 static void __exit motorek_exit_module(void)
190 platform_device_unregister(motorek_pdev);
191 platform_driver_unregister(&motorek_driver);
193 module_exit(motorek_exit_module);
196 MODULE_LICENSE("GPL v2");
197 MODULE_VERSION("0.1");
198 MODULE_AUTHOR("Michal Sojka <sojkam1@fel.cvut.cz>");