2 * Copyright 2011 Analog Devices Inc.
4 * Licensed under the GPL-2.
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>
19 #include "../trigger.h"
22 unsigned short id
, bit
;
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
},
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
},
53 struct bfin_tmr_state
{
54 struct iio_trigger
*trig
;
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
;
68 ret
= strict_strtoul(buf
, 10, &val
);
77 disable_gptimers(st
->t
->bit
);
82 val
= get_sclk() / val
;
88 set_gptimer_period(st
->t
->id
, val
);
89 set_gptimer_pwidth(st
->t
->id
, 1);
90 enable_gptimers(st
->t
->bit
);
93 return ret
? ret
: count
;
96 static ssize_t
iio_bfin_tmr_frequency_show(struct device
*dev
,
97 struct device_attribute
*attr
,
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
,
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
,
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);
135 static int iio_bfin_tmr_get_number(int irq
)
139 for (i
= 0; i
< MAX_BLACKFIN_GPTIMERS
; i
++)
140 if (iio_bfin_timer_code
[i
].irq
== irq
)
146 static const struct iio_trigger_ops iio_bfin_tmr_trigger_ops
= {
147 .owner
= THIS_MODULE
,
150 static int __devinit
iio_bfin_tmr_trigger_probe(struct platform_device
*pdev
)
152 struct bfin_tmr_state
*st
;
155 st
= kzalloc(sizeof(*st
), GFP_KERNEL
);
161 st
->irq
= platform_get_irq(pdev
, 0);
163 dev_err(&pdev
->dev
, "No IRQs specified");
168 ret
= iio_bfin_tmr_get_number(st
->irq
);
173 st
->t
= &iio_bfin_timer_code
[st
->timer_num
];
175 st
->trig
= iio_allocate_trigger("bfintmr%d", st
->timer_num
);
181 st
->trig
->private_data
= st
;
182 st
->trig
->ops
= &iio_bfin_tmr_trigger_ops
;
183 st
->trig
->dev
.groups
= iio_bfin_tmr_trigger_attr_groups
;
184 ret
= iio_trigger_register(st
->trig
);
188 ret
= request_irq(st
->irq
, iio_bfin_tmr_trigger_isr
,
189 0, st
->trig
->name
, st
);
192 "request IRQ-%d failed", st
->irq
);
196 set_gptimer_config(st
->t
->id
, OUT_DIS
| PWM_OUT
| PERIOD_CNT
| IRQ_ENA
);
198 dev_info(&pdev
->dev
, "iio trigger Blackfin TMR%d, IRQ-%d",
199 st
->timer_num
, st
->irq
);
200 platform_set_drvdata(pdev
, st
);
204 iio_trigger_unregister(st
->trig
);
206 iio_put_trigger(st
->trig
);
213 static int __devexit
iio_bfin_tmr_trigger_remove(struct platform_device
*pdev
)
215 struct bfin_tmr_state
*st
= platform_get_drvdata(pdev
);
217 disable_gptimers(st
->t
->bit
);
218 free_irq(st
->irq
, st
);
219 iio_trigger_unregister(st
->trig
);
220 iio_put_trigger(st
->trig
);
226 static struct platform_driver iio_bfin_tmr_trigger_driver
= {
228 .name
= "iio_bfin_tmr_trigger",
229 .owner
= THIS_MODULE
,
231 .probe
= iio_bfin_tmr_trigger_probe
,
232 .remove
= __devexit_p(iio_bfin_tmr_trigger_remove
),
235 static int __init
iio_bfin_tmr_trig_init(void)
237 return platform_driver_register(&iio_bfin_tmr_trigger_driver
);
239 module_init(iio_bfin_tmr_trig_init
);
241 static void __exit
iio_bfin_tmr_trig_exit(void)
243 platform_driver_unregister(&iio_bfin_tmr_trigger_driver
);
245 module_exit(iio_bfin_tmr_trig_exit
);
247 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
248 MODULE_DESCRIPTION("Blackfin system timer based trigger for the iio subsystem");
249 MODULE_LICENSE("GPL v2");
250 MODULE_ALIAS("platform:iio-trig-bfin-timer");