staging:iio:trigger handle name attr in core, remove old alloc and register any contr...
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / iio / trigger / iio-trig-bfin-timer.c
blob4f1729565582f3853efade06cdf024ad5eed06da
1 /*
2 * Copyright 2011 Analog Devices Inc.
4 * Licensed under the GPL-2.
6 */
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/platform_device.h>
11 #include <linux/slab.h>
12 #include <linux/interrupt.h>
13 #include <linux/irq.h>
14 #include <linux/delay.h>
16 #include <asm/gptimers.h>
18 #include "../iio.h"
19 #include "../trigger.h"
21 struct bfin_timer {
22 unsigned short id, bit;
23 unsigned long irqbit;
24 int irq;
28 * this covers all hardware timer configurations on
29 * all Blackfin derivatives out there today
32 static struct bfin_timer iio_bfin_timer_code[MAX_BLACKFIN_GPTIMERS] = {
33 {TIMER0_id, TIMER0bit, TIMER_STATUS_TIMIL0, IRQ_TIMER0},
34 {TIMER1_id, TIMER1bit, TIMER_STATUS_TIMIL1, IRQ_TIMER1},
35 {TIMER2_id, TIMER2bit, TIMER_STATUS_TIMIL2, IRQ_TIMER2},
36 #if (MAX_BLACKFIN_GPTIMERS > 3)
37 {TIMER3_id, TIMER3bit, TIMER_STATUS_TIMIL3, IRQ_TIMER3},
38 {TIMER4_id, TIMER4bit, TIMER_STATUS_TIMIL4, IRQ_TIMER4},
39 {TIMER5_id, TIMER5bit, TIMER_STATUS_TIMIL5, IRQ_TIMER5},
40 {TIMER6_id, TIMER6bit, TIMER_STATUS_TIMIL6, IRQ_TIMER6},
41 {TIMER7_id, TIMER7bit, TIMER_STATUS_TIMIL7, IRQ_TIMER7},
42 #endif
43 #if (MAX_BLACKFIN_GPTIMERS > 8)
44 {TIMER8_id, TIMER8bit, TIMER_STATUS_TIMIL8, IRQ_TIMER8},
45 {TIMER9_id, TIMER9bit, TIMER_STATUS_TIMIL9, IRQ_TIMER9},
46 {TIMER10_id, TIMER10bit, TIMER_STATUS_TIMIL10, IRQ_TIMER10},
47 #if (MAX_BLACKFIN_GPTIMERS > 11)
48 {TIMER11_id, TIMER11bit, TIMER_STATUS_TIMIL11, IRQ_TIMER11},
49 #endif
50 #endif
53 struct bfin_tmr_state {
54 struct iio_trigger *trig;
55 struct bfin_timer *t;
56 unsigned timer_num;
57 int irq;
60 static ssize_t iio_bfin_tmr_frequency_store(struct device *dev,
61 struct device_attribute *attr, const char *buf, size_t count)
63 struct iio_trigger *trig = dev_get_drvdata(dev);
64 struct bfin_tmr_state *st = trig->private_data;
65 long val;
66 int ret;
68 ret = strict_strtoul(buf, 10, &val);
69 if (ret)
70 goto error_ret;
72 if (val > 100000) {
73 ret = -EINVAL;
74 goto error_ret;
77 disable_gptimers(st->t->bit);
79 if (!val)
80 goto error_ret;
82 val = get_sclk() / val;
83 if (val <= 4) {
84 ret = -EINVAL;
85 goto error_ret;
88 set_gptimer_period(st->t->id, val);
89 set_gptimer_pwidth(st->t->id, 1);
90 enable_gptimers(st->t->bit);
92 error_ret:
93 return ret ? ret : count;
96 static ssize_t iio_bfin_tmr_frequency_show(struct device *dev,
97 struct device_attribute *attr,
98 char *buf)
100 struct iio_trigger *trig = dev_get_drvdata(dev);
101 struct bfin_tmr_state *st = trig->private_data;
103 return sprintf(buf, "%lu\n",
104 get_sclk() / get_gptimer_period(st->t->id));
107 static DEVICE_ATTR(frequency, S_IRUGO | S_IWUSR, iio_bfin_tmr_frequency_show,
108 iio_bfin_tmr_frequency_store);
110 static struct attribute *iio_bfin_tmr_trigger_attrs[] = {
111 &dev_attr_frequency.attr,
112 NULL,
115 static const struct attribute_group iio_bfin_tmr_trigger_attr_group = {
116 .attrs = iio_bfin_tmr_trigger_attrs,
119 static const struct attribute_group *iio_bfin_tmr_trigger_attr_groups[] = {
120 &iio_bfin_tmr_trigger_attr_group,
121 NULL
125 static irqreturn_t iio_bfin_tmr_trigger_isr(int irq, void *devid)
127 struct bfin_tmr_state *st = devid;
129 clear_gptimer_intr(st->t->id);
130 iio_trigger_poll(st->trig, 0);
132 return IRQ_HANDLED;
135 static int iio_bfin_tmr_get_number(int irq)
137 int i;
139 for (i = 0; i < MAX_BLACKFIN_GPTIMERS; i++)
140 if (iio_bfin_timer_code[i].irq == irq)
141 return i;
143 return -ENODEV;
146 static int __devinit iio_bfin_tmr_trigger_probe(struct platform_device *pdev)
148 struct bfin_tmr_state *st;
149 int ret;
151 st = kzalloc(sizeof(*st), GFP_KERNEL);
152 if (st == NULL) {
153 ret = -ENOMEM;
154 goto out;
157 st->irq = platform_get_irq(pdev, 0);
158 if (!st->irq) {
159 dev_err(&pdev->dev, "No IRQs specified");
160 ret = -ENODEV;
161 goto out1;
164 ret = iio_bfin_tmr_get_number(st->irq);
165 if (ret < 0)
166 goto out1;
168 st->timer_num = ret;
169 st->t = &iio_bfin_timer_code[st->timer_num];
171 st->trig = iio_allocate_trigger("bfintmr%d", st->timer_num);
172 if (!st->trig) {
173 ret = -ENOMEM;
174 goto out1;
177 st->trig->private_data = st;
178 st->trig->owner = THIS_MODULE;
179 st->trig->dev.groups = iio_bfin_tmr_trigger_attr_groups;
180 ret = iio_trigger_register(st->trig);
181 if (ret)
182 goto out2;
184 ret = request_irq(st->irq, iio_bfin_tmr_trigger_isr,
185 0, st->trig->name, st);
186 if (ret) {
187 dev_err(&pdev->dev,
188 "request IRQ-%d failed", st->irq);
189 goto out4;
192 set_gptimer_config(st->t->id, OUT_DIS | PWM_OUT | PERIOD_CNT | IRQ_ENA);
194 dev_info(&pdev->dev, "iio trigger Blackfin TMR%d, IRQ-%d",
195 st->timer_num, st->irq);
196 platform_set_drvdata(pdev, st);
198 return 0;
199 out4:
200 iio_trigger_unregister(st->trig);
201 out2:
202 iio_put_trigger(st->trig);
203 out1:
204 kfree(st);
205 out:
206 return ret;
209 static int __devexit iio_bfin_tmr_trigger_remove(struct platform_device *pdev)
211 struct bfin_tmr_state *st = platform_get_drvdata(pdev);
213 disable_gptimers(st->t->bit);
214 free_irq(st->irq, st);
215 iio_trigger_unregister(st->trig);
216 iio_put_trigger(st->trig);
217 kfree(st);
219 return 0;
222 static struct platform_driver iio_bfin_tmr_trigger_driver = {
223 .driver = {
224 .name = "iio_bfin_tmr_trigger",
225 .owner = THIS_MODULE,
227 .probe = iio_bfin_tmr_trigger_probe,
228 .remove = __devexit_p(iio_bfin_tmr_trigger_remove),
231 static int __init iio_bfin_tmr_trig_init(void)
233 return platform_driver_register(&iio_bfin_tmr_trigger_driver);
235 module_init(iio_bfin_tmr_trig_init);
237 static void __exit iio_bfin_tmr_trig_exit(void)
239 platform_driver_unregister(&iio_bfin_tmr_trigger_driver);
241 module_exit(iio_bfin_tmr_trig_exit);
243 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
244 MODULE_DESCRIPTION("Blackfin system timer based trigger for the iio subsystem");
245 MODULE_LICENSE("GPL v2");
246 MODULE_ALIAS("platform:iio-trig-bfin-timer");