1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include <linux/module.h>
20#include <linux/kernel.h>
21#include <linux/slab.h>
22#include <linux/init.h>
23#include <linux/errno.h>
24#include <linux/interrupt.h>
25#include <linux/device.h>
26#include <linux/mutex.h>
27
28#include <mach/dma.h>
29#include <mach/hardware.h>
30
31#include "ucb1x00.h"
32
33static DEFINE_MUTEX(ucb1x00_mutex);
34static LIST_HEAD(ucb1x00_drivers);
35static LIST_HEAD(ucb1x00_devices);
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53void ucb1x00_io_set_dir(struct ucb1x00 *ucb, unsigned int in, unsigned int out)
54{
55 unsigned long flags;
56
57 spin_lock_irqsave(&ucb->io_lock, flags);
58 ucb->io_dir |= out;
59 ucb->io_dir &= ~in;
60
61 ucb1x00_reg_write(ucb, UCB_IO_DIR, ucb->io_dir);
62 spin_unlock_irqrestore(&ucb->io_lock, flags);
63}
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81void ucb1x00_io_write(struct ucb1x00 *ucb, unsigned int set, unsigned int clear)
82{
83 unsigned long flags;
84
85 spin_lock_irqsave(&ucb->io_lock, flags);
86 ucb->io_out |= set;
87 ucb->io_out &= ~clear;
88
89 ucb1x00_reg_write(ucb, UCB_IO_DATA, ucb->io_out);
90 spin_unlock_irqrestore(&ucb->io_lock, flags);
91}
92
93
94
95
96
97
98
99
100
101
102
103
104
105unsigned int ucb1x00_io_read(struct ucb1x00 *ucb)
106{
107 return ucb1x00_reg_read(ucb, UCB_IO_DATA);
108}
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135void ucb1x00_adc_enable(struct ucb1x00 *ucb)
136{
137 down(&ucb->adc_sem);
138
139 ucb->adc_cr |= UCB_ADC_ENA;
140
141 ucb1x00_enable(ucb);
142 ucb1x00_reg_write(ucb, UCB_ADC_CR, ucb->adc_cr);
143}
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161unsigned int ucb1x00_adc_read(struct ucb1x00 *ucb, int adc_channel, int sync)
162{
163 unsigned int val;
164
165 if (sync)
166 adc_channel |= UCB_ADC_SYNC_ENA;
167
168 ucb1x00_reg_write(ucb, UCB_ADC_CR, ucb->adc_cr | adc_channel);
169 ucb1x00_reg_write(ucb, UCB_ADC_CR, ucb->adc_cr | adc_channel | UCB_ADC_START);
170
171 for (;;) {
172 val = ucb1x00_reg_read(ucb, UCB_ADC_DATA);
173 if (val & UCB_ADC_DAT_VAL)
174 break;
175
176 set_current_state(TASK_INTERRUPTIBLE);
177 schedule_timeout(1);
178 }
179
180 return UCB_ADC_DAT(val);
181}
182
183
184
185
186
187
188
189void ucb1x00_adc_disable(struct ucb1x00 *ucb)
190{
191 ucb->adc_cr &= ~UCB_ADC_ENA;
192 ucb1x00_reg_write(ucb, UCB_ADC_CR, ucb->adc_cr);
193 ucb1x00_disable(ucb);
194
195 up(&ucb->adc_sem);
196}
197
198
199
200
201
202
203
204
205
206static irqreturn_t ucb1x00_irq(int irqnr, void *devid)
207{
208 struct ucb1x00 *ucb = devid;
209 struct ucb1x00_irq *irq;
210 unsigned int isr, i;
211
212 ucb1x00_enable(ucb);
213 isr = ucb1x00_reg_read(ucb, UCB_IE_STATUS);
214 ucb1x00_reg_write(ucb, UCB_IE_CLEAR, isr);
215 ucb1x00_reg_write(ucb, UCB_IE_CLEAR, 0);
216
217 for (i = 0, irq = ucb->irq_handler; i < 16 && isr; i++, isr >>= 1, irq++)
218 if (isr & 1 && irq->fn)
219 irq->fn(i, irq->devid);
220 ucb1x00_disable(ucb);
221
222 return IRQ_HANDLED;
223}
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242int ucb1x00_hook_irq(struct ucb1x00 *ucb, unsigned int idx, void (*fn)(int, void *), void *devid)
243{
244 struct ucb1x00_irq *irq;
245 int ret = -EINVAL;
246
247 if (idx < 16) {
248 irq = ucb->irq_handler + idx;
249 ret = -EBUSY;
250
251 spin_lock_irq(&ucb->lock);
252 if (irq->fn == NULL) {
253 irq->devid = devid;
254 irq->fn = fn;
255 ret = 0;
256 }
257 spin_unlock_irq(&ucb->lock);
258 }
259 return ret;
260}
261
262
263
264
265
266
267
268
269
270
271
272void ucb1x00_enable_irq(struct ucb1x00 *ucb, unsigned int idx, int edges)
273{
274 unsigned long flags;
275
276 if (idx < 16) {
277 spin_lock_irqsave(&ucb->lock, flags);
278
279 ucb1x00_enable(ucb);
280 if (edges & UCB_RISING) {
281 ucb->irq_ris_enbl |= 1 << idx;
282 ucb1x00_reg_write(ucb, UCB_IE_RIS, ucb->irq_ris_enbl);
283 }
284 if (edges & UCB_FALLING) {
285 ucb->irq_fal_enbl |= 1 << idx;
286 ucb1x00_reg_write(ucb, UCB_IE_FAL, ucb->irq_fal_enbl);
287 }
288 ucb1x00_disable(ucb);
289 spin_unlock_irqrestore(&ucb->lock, flags);
290 }
291}
292
293
294
295
296
297
298
299
300
301void ucb1x00_disable_irq(struct ucb1x00 *ucb, unsigned int idx, int edges)
302{
303 unsigned long flags;
304
305 if (idx < 16) {
306 spin_lock_irqsave(&ucb->lock, flags);
307
308 ucb1x00_enable(ucb);
309 if (edges & UCB_RISING) {
310 ucb->irq_ris_enbl &= ~(1 << idx);
311 ucb1x00_reg_write(ucb, UCB_IE_RIS, ucb->irq_ris_enbl);
312 }
313 if (edges & UCB_FALLING) {
314 ucb->irq_fal_enbl &= ~(1 << idx);
315 ucb1x00_reg_write(ucb, UCB_IE_FAL, ucb->irq_fal_enbl);
316 }
317 ucb1x00_disable(ucb);
318 spin_unlock_irqrestore(&ucb->lock, flags);
319 }
320}
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335int ucb1x00_free_irq(struct ucb1x00 *ucb, unsigned int idx, void *devid)
336{
337 struct ucb1x00_irq *irq;
338 int ret;
339
340 if (idx >= 16)
341 goto bad;
342
343 irq = ucb->irq_handler + idx;
344 ret = -ENOENT;
345
346 spin_lock_irq(&ucb->lock);
347 if (irq->devid == devid) {
348 ucb->irq_ris_enbl &= ~(1 << idx);
349 ucb->irq_fal_enbl &= ~(1 << idx);
350
351 ucb1x00_enable(ucb);
352 ucb1x00_reg_write(ucb, UCB_IE_RIS, ucb->irq_ris_enbl);
353 ucb1x00_reg_write(ucb, UCB_IE_FAL, ucb->irq_fal_enbl);
354 ucb1x00_disable(ucb);
355
356 irq->fn = NULL;
357 irq->devid = NULL;
358 ret = 0;
359 }
360 spin_unlock_irq(&ucb->lock);
361 return ret;
362
363bad:
364 printk(KERN_ERR "Freeing bad UCB1x00 irq %d\n", idx);
365 return -EINVAL;
366}
367
368static int ucb1x00_add_dev(struct ucb1x00 *ucb, struct ucb1x00_driver *drv)
369{
370 struct ucb1x00_dev *dev;
371 int ret = -ENOMEM;
372
373 dev = kmalloc(sizeof(struct ucb1x00_dev), GFP_KERNEL);
374 if (dev) {
375 dev->ucb = ucb;
376 dev->drv = drv;
377
378 ret = drv->add(dev);
379
380 if (ret == 0) {
381 list_add(&dev->dev_node, &ucb->devs);
382 list_add(&dev->drv_node, &drv->devs);
383 } else {
384 kfree(dev);
385 }
386 }
387 return ret;
388}
389
390static void ucb1x00_remove_dev(struct ucb1x00_dev *dev)
391{
392 dev->drv->remove(dev);
393 list_del(&dev->dev_node);
394 list_del(&dev->drv_node);
395 kfree(dev);
396}
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417static int ucb1x00_detect_irq(struct ucb1x00 *ucb)
418{
419 unsigned long mask;
420
421 mask = probe_irq_on();
422 if (!mask) {
423 probe_irq_off(mask);
424 return NO_IRQ;
425 }
426
427
428
429
430 ucb1x00_reg_write(ucb, UCB_IE_RIS, UCB_IE_ADC);
431 ucb1x00_reg_write(ucb, UCB_IE_FAL, UCB_IE_ADC);
432 ucb1x00_reg_write(ucb, UCB_IE_CLEAR, 0xffff);
433 ucb1x00_reg_write(ucb, UCB_IE_CLEAR, 0);
434
435
436
437
438 ucb1x00_reg_write(ucb, UCB_ADC_CR, UCB_ADC_ENA);
439 ucb1x00_reg_write(ucb, UCB_ADC_CR, UCB_ADC_ENA | UCB_ADC_START);
440
441
442
443
444 while ((ucb1x00_reg_read(ucb, UCB_ADC_DATA) & UCB_ADC_DAT_VAL) == 0);
445 ucb1x00_reg_write(ucb, UCB_ADC_CR, 0);
446
447
448
449
450 ucb1x00_reg_write(ucb, UCB_IE_RIS, 0);
451 ucb1x00_reg_write(ucb, UCB_IE_FAL, 0);
452 ucb1x00_reg_write(ucb, UCB_IE_CLEAR, 0xffff);
453 ucb1x00_reg_write(ucb, UCB_IE_CLEAR, 0);
454
455
456
457
458 return probe_irq_off(mask);
459}
460
461static void ucb1x00_release(struct device *dev)
462{
463 struct ucb1x00 *ucb = classdev_to_ucb1x00(dev);
464 kfree(ucb);
465}
466
467static struct class ucb1x00_class = {
468 .name = "ucb1x00",
469 .dev_release = ucb1x00_release,
470};
471
472static int ucb1x00_probe(struct mcp *mcp)
473{
474 struct ucb1x00 *ucb;
475 struct ucb1x00_driver *drv;
476 unsigned int id;
477 int ret = -ENODEV;
478
479 mcp_enable(mcp);
480 id = mcp_reg_read(mcp, UCB_ID);
481
482 if (id != UCB_ID_1200 && id != UCB_ID_1300 && id != UCB_ID_TC35143) {
483 printk(KERN_WARNING "UCB1x00 ID not found: %04x\n", id);
484 goto err_disable;
485 }
486
487 ucb = kzalloc(sizeof(struct ucb1x00), GFP_KERNEL);
488 ret = -ENOMEM;
489 if (!ucb)
490 goto err_disable;
491
492
493 ucb->dev.class = &ucb1x00_class;
494 ucb->dev.parent = &mcp->attached_device;
495 dev_set_name(&ucb->dev, "ucb1x00");
496
497 spin_lock_init(&ucb->lock);
498 spin_lock_init(&ucb->io_lock);
499 sema_init(&ucb->adc_sem, 1);
500
501 ucb->id = id;
502 ucb->mcp = mcp;
503 ucb->irq = ucb1x00_detect_irq(ucb);
504 if (ucb->irq == NO_IRQ) {
505 printk(KERN_ERR "UCB1x00: IRQ probe failed\n");
506 ret = -ENODEV;
507 goto err_free;
508 }
509
510 ret = request_irq(ucb->irq, ucb1x00_irq, IRQF_TRIGGER_RISING,
511 "UCB1x00", ucb);
512 if (ret) {
513 printk(KERN_ERR "ucb1x00: unable to grab irq%d: %d\n",
514 ucb->irq, ret);
515 goto err_free;
516 }
517
518 mcp_set_drvdata(mcp, ucb);
519
520 ret = device_register(&ucb->dev);
521 if (ret)
522 goto err_irq;
523
524 INIT_LIST_HEAD(&ucb->devs);
525 mutex_lock(&ucb1x00_mutex);
526 list_add(&ucb->node, &ucb1x00_devices);
527 list_for_each_entry(drv, &ucb1x00_drivers, node) {
528 ucb1x00_add_dev(ucb, drv);
529 }
530 mutex_unlock(&ucb1x00_mutex);
531 goto out;
532
533 err_irq:
534 free_irq(ucb->irq, ucb);
535 err_free:
536 kfree(ucb);
537 err_disable:
538 mcp_disable(mcp);
539 out:
540 return ret;
541}
542
543static void ucb1x00_remove(struct mcp *mcp)
544{
545 struct ucb1x00 *ucb = mcp_get_drvdata(mcp);
546 struct list_head *l, *n;
547
548 mutex_lock(&ucb1x00_mutex);
549 list_del(&ucb->node);
550 list_for_each_safe(l, n, &ucb->devs) {
551 struct ucb1x00_dev *dev = list_entry(l, struct ucb1x00_dev, dev_node);
552 ucb1x00_remove_dev(dev);
553 }
554 mutex_unlock(&ucb1x00_mutex);
555
556 free_irq(ucb->irq, ucb);
557 device_unregister(&ucb->dev);
558}
559
560int ucb1x00_register_driver(struct ucb1x00_driver *drv)
561{
562 struct ucb1x00 *ucb;
563
564 INIT_LIST_HEAD(&drv->devs);
565 mutex_lock(&ucb1x00_mutex);
566 list_add(&drv->node, &ucb1x00_drivers);
567 list_for_each_entry(ucb, &ucb1x00_devices, node) {
568 ucb1x00_add_dev(ucb, drv);
569 }
570 mutex_unlock(&ucb1x00_mutex);
571 return 0;
572}
573
574void ucb1x00_unregister_driver(struct ucb1x00_driver *drv)
575{
576 struct list_head *n, *l;
577
578 mutex_lock(&ucb1x00_mutex);
579 list_del(&drv->node);
580 list_for_each_safe(l, n, &drv->devs) {
581 struct ucb1x00_dev *dev = list_entry(l, struct ucb1x00_dev, drv_node);
582 ucb1x00_remove_dev(dev);
583 }
584 mutex_unlock(&ucb1x00_mutex);
585}
586
587static int ucb1x00_suspend(struct mcp *mcp, pm_message_t state)
588{
589 struct ucb1x00 *ucb = mcp_get_drvdata(mcp);
590 struct ucb1x00_dev *dev;
591
592 mutex_lock(&ucb1x00_mutex);
593 list_for_each_entry(dev, &ucb->devs, dev_node) {
594 if (dev->drv->suspend)
595 dev->drv->suspend(dev, state);
596 }
597 mutex_unlock(&ucb1x00_mutex);
598 return 0;
599}
600
601static int ucb1x00_resume(struct mcp *mcp)
602{
603 struct ucb1x00 *ucb = mcp_get_drvdata(mcp);
604 struct ucb1x00_dev *dev;
605
606 mutex_lock(&ucb1x00_mutex);
607 list_for_each_entry(dev, &ucb->devs, dev_node) {
608 if (dev->drv->resume)
609 dev->drv->resume(dev);
610 }
611 mutex_unlock(&ucb1x00_mutex);
612 return 0;
613}
614
615static struct mcp_driver ucb1x00_driver = {
616 .drv = {
617 .name = "ucb1x00",
618 },
619 .probe = ucb1x00_probe,
620 .remove = ucb1x00_remove,
621 .suspend = ucb1x00_suspend,
622 .resume = ucb1x00_resume,
623};
624
625static int __init ucb1x00_init(void)
626{
627 int ret = class_register(&ucb1x00_class);
628 if (ret == 0) {
629 ret = mcp_driver_register(&ucb1x00_driver);
630 if (ret)
631 class_unregister(&ucb1x00_class);
632 }
633 return ret;
634}
635
636static void __exit ucb1x00_exit(void)
637{
638 mcp_driver_unregister(&ucb1x00_driver);
639 class_unregister(&ucb1x00_class);
640}
641
642module_init(ucb1x00_init);
643module_exit(ucb1x00_exit);
644
645EXPORT_SYMBOL(ucb1x00_io_set_dir);
646EXPORT_SYMBOL(ucb1x00_io_write);
647EXPORT_SYMBOL(ucb1x00_io_read);
648
649EXPORT_SYMBOL(ucb1x00_adc_enable);
650EXPORT_SYMBOL(ucb1x00_adc_read);
651EXPORT_SYMBOL(ucb1x00_adc_disable);
652
653EXPORT_SYMBOL(ucb1x00_hook_irq);
654EXPORT_SYMBOL(ucb1x00_free_irq);
655EXPORT_SYMBOL(ucb1x00_enable_irq);
656EXPORT_SYMBOL(ucb1x00_disable_irq);
657
658EXPORT_SYMBOL(ucb1x00_register_driver);
659EXPORT_SYMBOL(ucb1x00_unregister_driver);
660
661MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
662MODULE_DESCRIPTION("UCB1x00 core driver");
663MODULE_LICENSE("GPL");
664