]> rtime.felk.cvut.cz Git - shark/motorek-5200.git/blob - motorek.c
Initial commit
[shark/motorek-5200.git] / motorek.c
1 #include <linux/module.h>
2 #include <linux/platform_device.h>
3 #include <linux/mod_devicetable.h>
4 #include <linux/of.h>
5 #include <asm-powerpc/mpc52xx.h>
6 #include <linux/interrupt.h>
7
8 struct motorek {
9         struct mpc52xx_gpt *pwmf, *pwmb;
10         atomic_t pos;
11         int irq;
12 };
13
14 static ssize_t show_position(struct device *dev,
15                             struct device_attribute *attr, char *buf)
16 {
17         struct platform_device  *pdev = to_platform_device(dev);
18         struct motorek          *m = platform_get_drvdata(pdev);
19
20         int len = snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&m->pos));
21         return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
22 }
23
24 DEVICE_ATTR(position,0444,show_position,NULL);
25
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
31
32 #define MPC52xx_GPT_PWM_OP        (1<<8)
33
34 /* Base clock 132 MHz
35  * (/proc/device-tree/cpus/PowerPC,5200@0/bus-frequency) */
36 #define PWM_PERIOD 6600         /* = 132 000 kHz / 20 kHz  */
37
38
39 static void pwm_width(struct mpc52xx_gpt *gpt, u16 width)
40 {
41         gpt->pwm = (width<<16) | MPC52xx_GPT_PWM_OP;
42 }
43
44 static void __devinit pwm_init(struct mpc52xx_gpt *gpt)
45 {
46         gpt->count = (1<<16) | PWM_PERIOD;
47         pwm_width(gpt, 0);
48         gpt->mode = MPC52xx_GPT_MODE_PWM;
49 }
50
51 static void  pwm_done(struct mpc52xx_gpt *gpt)
52 {
53         gpt->mode = 0;
54         gpt->count = 0;
55         pwm_width(gpt, 0);
56 }
57
58 static void motorek_action(struct motorek *m, int action_permile)
59 {
60         if (action_permile >= 0) {
61                 pwm_width(m->pwmb, 0);
62                 pwm_width(m->pwmf, +action_permile*PWM_PERIOD/1000);
63         } else {
64                 pwm_width(m->pwmf, 0);
65                 pwm_width(m->pwmb, -action_permile*PWM_PERIOD/1000);
66         }
67 }
68
69 static int __devinit motorek_init(struct motorek *m)
70 {
71         pwm_init(m->pwmf);
72         pwm_init(m->pwmb);
73         motorek_action(m, +45);
74         return 0;
75 }
76
77 static int motorek_done(struct motorek *m)
78 {
79         pwm_done(m->pwmf);
80         pwm_done(m->pwmb);
81         return 0;
82 }
83
84 static irqreturn_t motorek_irq(int irq, void *dev_id)
85 {
86         struct motorek *m = dev_id;
87         atomic_inc(&m->pos);
88         return IRQ_HANDLED;
89 }
90
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", },
95         {}
96 };
97
98 static int __devinit motorek_probe(struct platform_device *dev)
99 {
100         struct device_node *np;
101         struct motorek *m;
102         int err;
103
104         m = kzalloc(sizeof(*m), GFP_KERNEL);
105         if (!m)
106                 return -ENOMEM;
107
108         for_each_matching_node(np, mpc5200_gpt_ids) {
109                 const void *prop;
110                 int i;
111                 
112                 prop = of_get_property(np, "cell-index", NULL);
113                 if (prop) {
114                         i = *(u32 *)prop;
115                         switch (i) {
116                         case 1:
117                                 m->pwmf = of_iomap(np, 0);
118                                 break;
119                         case 2:
120                                 m->pwmb = of_iomap(np, 0);
121                                 break;
122                         }
123                 }
124         }
125         if (!m->pwmf || !m->pwmb) {
126                 printk(KERN_ERR "%s() mmap failed\n", __func__);
127                 return -ENXIO;
128         }
129
130 /*      /\* FIXME: This should be specified in device-tree *\/ */
131 /*      m->irq = irq_create_of_mapping( */
132
133 /*      err = request_irq(m->irq, motorek_irq, 0, "motorek", m); */
134 /*      if (err) */
135 /*              return err; */
136
137         motorek_init(m);
138
139         platform_set_drvdata(dev, m);
140
141         err = device_create_file(&dev->dev,&dev_attr_position);
142         if (err)
143                 return err;
144
145         return 0;
146 }
147
148 static int __devexit motorek_remove(struct platform_device *dev)
149 {
150         struct motorek *m;
151         m = platform_get_drvdata(dev);
152
153         motorek_done(m);
154         return 0;
155 }
156
157
158
159 static struct platform_driver motorek_driver = {
160         .probe          = motorek_probe,
161         .remove         = __devexit_p(motorek_remove),
162         .driver         = {
163                 .name   = "motorek",
164                 .owner  = THIS_MODULE,
165         },
166 };
167
168
169 static struct platform_device *motorek_pdev;
170
171 static int __init motorek_init_module(void)
172 {
173         int ret;
174         motorek_pdev = platform_device_alloc("motorek", 0);
175         if (!motorek_pdev)
176                 return -ENOMEM;
177         ret = platform_device_add(motorek_pdev);
178         if (ret) {
179                 platform_device_put(motorek_pdev);
180                 return ret;
181         }
182
183         ret = platform_driver_register(&motorek_driver);
184         return 0;
185 }
186 module_init(motorek_init_module);
187
188 static void __exit motorek_exit_module(void)
189 {
190         platform_device_unregister(motorek_pdev);
191         platform_driver_unregister(&motorek_driver);
192 }
193 module_exit(motorek_exit_module);
194
195
196 MODULE_LICENSE("GPL v2");
197 MODULE_VERSION("0.1");
198 MODULE_AUTHOR("Michal Sojka <sojkam1@fel.cvut.cz>");