]> rtime.felk.cvut.cz Git - shark/motorek-5200.git/blob - motorek.c
Setup PWMs according to OF
[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 <linux/of_platform.h>
6 #include <asm-powerpc/mpc52xx.h>
7 #include <linux/interrupt.h>
8
9 struct motorek {
10         struct mpc52xx_gpt *pwmf, *pwmb;
11         atomic_t pos;
12         int irq;
13         int action;
14 };
15
16 static ssize_t show_position(struct device *dev,
17                             struct device_attribute *attr, char *buf)
18 {
19         struct platform_device  *pdev = to_platform_device(dev);
20         struct motorek          *m = platform_get_drvdata(pdev);
21
22         int len = snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&m->pos));
23         return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
24 }
25
26 static ssize_t show_action(struct device *dev,
27                            struct device_attribute *attr, char *buf)
28 {
29         struct platform_device  *pdev = to_platform_device(dev);
30         struct motorek          *m = platform_get_drvdata(pdev);
31
32         int len = snprintf(buf, PAGE_SIZE, "%d\n", m->action);
33         return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
34 }
35
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)
39 {
40         struct platform_device  *pdev = to_platform_device(dev);
41         struct motorek          *m = platform_get_drvdata(pdev);
42
43         int a;
44         sscanf(buf, "%d", &a);
45         motorek_action(m, a);
46         return strnlen(buf, PAGE_SIZE);
47 }
48
49 DEVICE_ATTR(position,0444,show_position,NULL);
50 DEVICE_ATTR(action,0644,show_action,store_action);
51
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
57
58 #define MPC52xx_GPT_PWM_OP        (1<<8)
59
60 /* Base clock 132 MHz
61  * (/proc/device-tree/cpus/PowerPC,5200@0/bus-frequency) */
62 #define PWM_PERIOD 6600         /* = 132 000 kHz / 20 kHz  */
63
64
65 static void pwm_width(struct mpc52xx_gpt *gpt, u16 width)
66 {
67         out_be32(&gpt->pwm, (width<<16) | MPC52xx_GPT_PWM_OP);
68 }
69
70 static void __devinit pwm_init(struct mpc52xx_gpt *gpt)
71 {
72         out_be32(&gpt->count, (1<<16) | PWM_PERIOD);
73         pwm_width(gpt, 0);
74         out_be32(&gpt->mode, MPC52xx_GPT_MODE_PWM);
75 }
76
77 static void  pwm_done(struct mpc52xx_gpt *gpt)
78 {
79         out_be32(&gpt->mode, 0);
80         out_be32(&gpt->count, 0);
81         pwm_width(gpt, 0);
82 }
83
84 static void motorek_action(struct motorek *m, int action_permile)
85 {
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);
90         } else {
91                 pwm_width(m->pwmf, 0);
92                 pwm_width(m->pwmb, -action_permile*PWM_PERIOD/1000);
93         }
94 }
95
96 static int __devinit motorek_init(struct motorek *m)
97 {
98         pwm_init(m->pwmf);
99         pwm_init(m->pwmb);
100         //motorek_action(m, +0);
101         return 0;
102 }
103
104 static int motorek_done(struct motorek *m)
105 {
106         pwm_done(m->pwmf);
107         pwm_done(m->pwmb);
108         return 0;
109 }
110
111 static irqreturn_t motorek_irq(int irq, void *dev_id)
112 {
113         struct motorek *m = dev_id;
114         atomic_inc(&m->pos);
115         return IRQ_HANDLED;
116 }
117
118 struct mpc52xx_gpt __iomem *iomap_gpt_by_phandle(const phandle *phandle)
119 {
120         struct device_node *np;
121         struct mpc52xx_gpt __iomem *gpt;
122
123         if (!phandle)
124                 return NULL;
125
126         np = of_find_node_by_phandle(*phandle);
127         if (!np)
128                 return NULL;
129         gpt = of_iomap(np, 0);
130         of_node_put(np);
131         return gpt;
132 }
133
134 static int __devinit motorek_probe(struct of_device* dev,
135                                    const struct of_device_id *match)
136 {
137         struct device_node *dn = dev->node;
138         struct motorek *m;
139         int err;
140
141         m = kzalloc(sizeof(*m), GFP_KERNEL);
142         if (!m)
143                 return -ENOMEM;
144
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));
147         
148         if (!m->pwmf || !m->pwmb) {
149                 printk(KERN_ERR "%s() mmap failed\n", __func__);
150                 return -ENXIO;
151         }
152
153 /*      /\* FIXME: This should be specified in device-tree *\/ */
154 /*      m->irq = irq_create_of_mapping( */
155
156 /*      err = request_irq(m->irq, motorek_irq, 0, "motorek", m); */
157 /*      if (err) */
158 /*              return err; */
159
160         motorek_init(m);
161
162         platform_set_drvdata(dev, m);
163
164         err = device_create_file(&dev->dev,&dev_attr_position);
165         if (err)
166                 return err;
167         err = device_create_file(&dev->dev,&dev_attr_action);
168         if (err)
169                 return err;
170
171         printk(KERN_NOTICE "Motorek initialized\n");
172
173         return 0;
174 }
175
176 static int __devexit motorek_remove(struct of_device* dev)
177 {
178         struct motorek *m;
179         m = platform_get_drvdata(dev);
180
181         printk(KERN_NOTICE "Removing motorek\n");
182
183         motorek_done(m);
184         return 0;
185 }
186
187 static struct of_device_id motorek_match[] = {
188         { .type = "motorek", },
189         {},
190 };
191 static struct of_platform_driver motorek_driver = {
192         .owner          = THIS_MODULE,
193         .name           = "motorek",
194         .match_table    = motorek_match,
195         .probe          = motorek_probe,
196         .remove         = __devexit_p(motorek_remove),
197 };
198
199
200 static int __init motorek_init_module(void)
201 {
202         int ret;
203         struct of_device *dev;
204         struct device_node *dn;
205
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);
209                         if (!dev)
210                                 return -ENOMEM;
211                 }
212         };
213
214         ret = of_register_platform_driver(&motorek_driver);
215         return ret;
216 }
217 module_init(motorek_init_module);
218
219 static void __exit motorek_exit_module(void)
220 {
221         struct of_device *dev;
222         struct device_node *dn;
223
224         for_each_node_by_type(dn, "motorek") {
225                 while ((dev = of_find_device_by_node(dn))) {
226                         of_device_unregister(dev);
227                 }
228         }
229         of_unregister_platform_driver(&motorek_driver);
230 }
231 module_exit(motorek_exit_module);
232
233
234 MODULE_LICENSE("GPL v2");
235 MODULE_VERSION("0.1");
236 MODULE_AUTHOR("Michal Sojka <sojkam1@fel.cvut.cz>");