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;
16 static ssize_t show_position(struct device *dev,
17 struct device_attribute *attr, char *buf)
19 struct platform_device *pdev = to_platform_device(dev);
20 struct motorek *m = platform_get_drvdata(pdev);
22 int len = snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&m->pos));
23 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
26 static ssize_t show_action(struct device *dev,
27 struct device_attribute *attr, char *buf)
29 struct platform_device *pdev = to_platform_device(dev);
30 struct motorek *m = platform_get_drvdata(pdev);
32 int len = snprintf(buf, PAGE_SIZE, "%d\n", m->action);
33 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
36 static void motorek_action(struct motorek *m, int action_permile);
37 static ssize_t store_action(struct device *dev, struct device_attribute *attr,
38 const char *buf, size_t count)
40 struct platform_device *pdev = to_platform_device(dev);
41 struct motorek *m = platform_get_drvdata(pdev);
44 sscanf(buf, "%d", &a);
46 return strnlen(buf, PAGE_SIZE);
49 DEVICE_ATTR(position,0444,show_position,NULL);
50 DEVICE_ATTR(action,0644,show_action,store_action);
52 #define MPC52xx_GPT_MODE_DISABLED 0
53 #define MPC52xx_GPT_MODE_INCAPT 1
54 #define MPC52xx_GPT_MODE_OUTCMP 2
55 #define MPC52xx_GPT_MODE_PWM 3
56 #define MPC52xx_GPT_MODE_GPIO 4
58 #define MPC52xx_GPT_PWM_OP (1<<8)
61 * (/proc/device-tree/cpus/PowerPC,5200@0/bus-frequency) */
62 #define PWM_PERIOD 6600 /* = 132 000 kHz / 20 kHz */
65 static void pwm_width(struct mpc52xx_gpt *gpt, u16 width)
67 out_be32(&gpt->pwm, (width<<16) | MPC52xx_GPT_PWM_OP);
70 static void __devinit pwm_init(struct mpc52xx_gpt *gpt)
72 out_be32(&gpt->count, (1<<16) | PWM_PERIOD);
74 out_be32(&gpt->mode, MPC52xx_GPT_MODE_PWM);
77 static void pwm_done(struct mpc52xx_gpt *gpt)
79 out_be32(&gpt->mode, 0);
80 out_be32(&gpt->count, 0);
84 static void motorek_action(struct motorek *m, int action_permile)
86 m->action = action_permile;
87 if (action_permile >= 0) {
88 pwm_width(m->pwmb, 0);
89 pwm_width(m->pwmf, +action_permile*PWM_PERIOD/1000);
91 pwm_width(m->pwmf, 0);
92 pwm_width(m->pwmb, -action_permile*PWM_PERIOD/1000);
96 static int __devinit motorek_init(struct motorek *m)
100 //motorek_action(m, +0);
104 static int motorek_done(struct motorek *m)
111 static irqreturn_t motorek_irq(int irq, void *dev_id)
113 struct motorek *m = dev_id;
118 struct mpc52xx_gpt __iomem *iomap_gpt_by_phandle(const phandle *phandle)
120 struct device_node *np;
121 struct mpc52xx_gpt __iomem *gpt;
126 np = of_find_node_by_phandle(*phandle);
129 gpt = of_iomap(np, 0);
134 static int __devinit motorek_probe(struct of_device* dev,
135 const struct of_device_id *match)
137 struct device_node *dn = dev->node;
141 m = kzalloc(sizeof(*m), GFP_KERNEL);
145 m->pwmf = iomap_gpt_by_phandle(of_get_property(dn, "pwmf", NULL));
146 m->pwmb = iomap_gpt_by_phandle(of_get_property(dn, "pwmb", NULL));
148 if (!m->pwmf || !m->pwmb) {
149 printk(KERN_ERR "%s() mmap failed\n", __func__);
153 /* /\* FIXME: This should be specified in device-tree *\/ */
154 /* m->irq = irq_create_of_mapping( */
156 /* err = request_irq(m->irq, motorek_irq, 0, "motorek", m); */
162 platform_set_drvdata(dev, m);
164 err = device_create_file(&dev->dev,&dev_attr_position);
167 err = device_create_file(&dev->dev,&dev_attr_action);
171 printk(KERN_NOTICE "Motorek initialized\n");
176 static int __devexit motorek_remove(struct of_device* dev)
179 m = platform_get_drvdata(dev);
181 printk(KERN_NOTICE "Removing motorek\n");
187 static struct of_device_id motorek_match[] = {
188 { .type = "motorek", },
191 static struct of_platform_driver motorek_driver = {
192 .owner = THIS_MODULE,
194 .match_table = motorek_match,
195 .probe = motorek_probe,
196 .remove = __devexit_p(motorek_remove),
200 static int __init motorek_init_module(void)
203 struct of_device *dev;
204 struct device_node *dn;
206 for_each_node_by_type(dn, "motorek") {
207 if (!of_find_device_by_node(dn)) {
208 dev = of_platform_device_create(dn, "motorek", NULL);
214 ret = of_register_platform_driver(&motorek_driver);
217 module_init(motorek_init_module);
219 static void __exit motorek_exit_module(void)
221 struct of_device *dev;
222 struct device_node *dn;
224 for_each_node_by_type(dn, "motorek") {
225 while ((dev = of_find_device_by_node(dn))) {
226 of_device_unregister(dev);
229 of_unregister_platform_driver(&motorek_driver);
231 module_exit(motorek_exit_module);
234 MODULE_LICENSE("GPL v2");
235 MODULE_VERSION("0.1");
236 MODULE_AUTHOR("Michal Sojka <sojkam1@fel.cvut.cz>");