2 * Copyright 2009-2010 Pengutronix
3 * Uwe Kleine-Koenig <u.kleine-koenig@pengutronix.de>
5 * loosely based on an earlier driver that has
6 * Copyright 2009 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License version 2 as published by the
10 * Free Software Foundation.
13 #include <linux/slab.h>
14 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/mutex.h>
17 #include <linux/interrupt.h>
18 #include <linux/spi/spi.h>
19 #include <linux/mfd/core.h>
20 #include <linux/mfd/mc13xxx.h>
23 struct spi_device
*spidev
;
27 irq_handler_t irqhandler
[MC13XXX_NUM_IRQ
];
28 void *irqdata
[MC13XXX_NUM_IRQ
];
32 struct mc13xxx mc13xxx
;
37 struct mc13xxx
*mc13783_to_mc13xxx(struct mc13783
*mc13783
)
39 return &mc13783
->mc13xxx
;
41 EXPORT_SYMBOL(mc13783_to_mc13xxx
);
43 #define MC13XXX_IRQSTAT0 0
44 #define MC13XXX_IRQSTAT0_ADCDONEI (1 << 0)
45 #define MC13XXX_IRQSTAT0_ADCBISDONEI (1 << 1)
46 #define MC13XXX_IRQSTAT0_TSI (1 << 2)
47 #define MC13783_IRQSTAT0_WHIGHI (1 << 3)
48 #define MC13783_IRQSTAT0_WLOWI (1 << 4)
49 #define MC13XXX_IRQSTAT0_CHGDETI (1 << 6)
50 #define MC13783_IRQSTAT0_CHGOVI (1 << 7)
51 #define MC13XXX_IRQSTAT0_CHGREVI (1 << 8)
52 #define MC13XXX_IRQSTAT0_CHGSHORTI (1 << 9)
53 #define MC13XXX_IRQSTAT0_CCCVI (1 << 10)
54 #define MC13XXX_IRQSTAT0_CHGCURRI (1 << 11)
55 #define MC13XXX_IRQSTAT0_BPONI (1 << 12)
56 #define MC13XXX_IRQSTAT0_LOBATLI (1 << 13)
57 #define MC13XXX_IRQSTAT0_LOBATHI (1 << 14)
58 #define MC13783_IRQSTAT0_UDPI (1 << 15)
59 #define MC13783_IRQSTAT0_USBI (1 << 16)
60 #define MC13783_IRQSTAT0_IDI (1 << 19)
61 #define MC13783_IRQSTAT0_SE1I (1 << 21)
62 #define MC13783_IRQSTAT0_CKDETI (1 << 22)
63 #define MC13783_IRQSTAT0_UDMI (1 << 23)
65 #define MC13XXX_IRQMASK0 1
66 #define MC13XXX_IRQMASK0_ADCDONEM MC13XXX_IRQSTAT0_ADCDONEI
67 #define MC13XXX_IRQMASK0_ADCBISDONEM MC13XXX_IRQSTAT0_ADCBISDONEI
68 #define MC13XXX_IRQMASK0_TSM MC13XXX_IRQSTAT0_TSI
69 #define MC13783_IRQMASK0_WHIGHM MC13783_IRQSTAT0_WHIGHI
70 #define MC13783_IRQMASK0_WLOWM MC13783_IRQSTAT0_WLOWI
71 #define MC13XXX_IRQMASK0_CHGDETM MC13XXX_IRQSTAT0_CHGDETI
72 #define MC13783_IRQMASK0_CHGOVM MC13783_IRQSTAT0_CHGOVI
73 #define MC13XXX_IRQMASK0_CHGREVM MC13XXX_IRQSTAT0_CHGREVI
74 #define MC13XXX_IRQMASK0_CHGSHORTM MC13XXX_IRQSTAT0_CHGSHORTI
75 #define MC13XXX_IRQMASK0_CCCVM MC13XXX_IRQSTAT0_CCCVI
76 #define MC13XXX_IRQMASK0_CHGCURRM MC13XXX_IRQSTAT0_CHGCURRI
77 #define MC13XXX_IRQMASK0_BPONM MC13XXX_IRQSTAT0_BPONI
78 #define MC13XXX_IRQMASK0_LOBATLM MC13XXX_IRQSTAT0_LOBATLI
79 #define MC13XXX_IRQMASK0_LOBATHM MC13XXX_IRQSTAT0_LOBATHI
80 #define MC13783_IRQMASK0_UDPM MC13783_IRQSTAT0_UDPI
81 #define MC13783_IRQMASK0_USBM MC13783_IRQSTAT0_USBI
82 #define MC13783_IRQMASK0_IDM MC13783_IRQSTAT0_IDI
83 #define MC13783_IRQMASK0_SE1M MC13783_IRQSTAT0_SE1I
84 #define MC13783_IRQMASK0_CKDETM MC13783_IRQSTAT0_CKDETI
85 #define MC13783_IRQMASK0_UDMM MC13783_IRQSTAT0_UDMI
87 #define MC13XXX_IRQSTAT1 3
88 #define MC13XXX_IRQSTAT1_1HZI (1 << 0)
89 #define MC13XXX_IRQSTAT1_TODAI (1 << 1)
90 #define MC13783_IRQSTAT1_ONOFD1I (1 << 3)
91 #define MC13783_IRQSTAT1_ONOFD2I (1 << 4)
92 #define MC13783_IRQSTAT1_ONOFD3I (1 << 5)
93 #define MC13XXX_IRQSTAT1_SYSRSTI (1 << 6)
94 #define MC13XXX_IRQSTAT1_RTCRSTI (1 << 7)
95 #define MC13XXX_IRQSTAT1_PCI (1 << 8)
96 #define MC13XXX_IRQSTAT1_WARMI (1 << 9)
97 #define MC13XXX_IRQSTAT1_MEMHLDI (1 << 10)
98 #define MC13783_IRQSTAT1_PWRRDYI (1 << 11)
99 #define MC13XXX_IRQSTAT1_THWARNLI (1 << 12)
100 #define MC13XXX_IRQSTAT1_THWARNHI (1 << 13)
101 #define MC13XXX_IRQSTAT1_CLKI (1 << 14)
102 #define MC13783_IRQSTAT1_SEMAFI (1 << 15)
103 #define MC13783_IRQSTAT1_MC2BI (1 << 17)
104 #define MC13783_IRQSTAT1_HSDETI (1 << 18)
105 #define MC13783_IRQSTAT1_HSLI (1 << 19)
106 #define MC13783_IRQSTAT1_ALSPTHI (1 << 20)
107 #define MC13783_IRQSTAT1_AHSSHORTI (1 << 21)
109 #define MC13XXX_IRQMASK1 4
110 #define MC13XXX_IRQMASK1_1HZM MC13XXX_IRQSTAT1_1HZI
111 #define MC13XXX_IRQMASK1_TODAM MC13XXX_IRQSTAT1_TODAI
112 #define MC13783_IRQMASK1_ONOFD1M MC13783_IRQSTAT1_ONOFD1I
113 #define MC13783_IRQMASK1_ONOFD2M MC13783_IRQSTAT1_ONOFD2I
114 #define MC13783_IRQMASK1_ONOFD3M MC13783_IRQSTAT1_ONOFD3I
115 #define MC13XXX_IRQMASK1_SYSRSTM MC13XXX_IRQSTAT1_SYSRSTI
116 #define MC13XXX_IRQMASK1_RTCRSTM MC13XXX_IRQSTAT1_RTCRSTI
117 #define MC13XXX_IRQMASK1_PCM MC13XXX_IRQSTAT1_PCI
118 #define MC13XXX_IRQMASK1_WARMM MC13XXX_IRQSTAT1_WARMI
119 #define MC13XXX_IRQMASK1_MEMHLDM MC13XXX_IRQSTAT1_MEMHLDI
120 #define MC13783_IRQMASK1_PWRRDYM MC13783_IRQSTAT1_PWRRDYI
121 #define MC13XXX_IRQMASK1_THWARNLM MC13XXX_IRQSTAT1_THWARNLI
122 #define MC13XXX_IRQMASK1_THWARNHM MC13XXX_IRQSTAT1_THWARNHI
123 #define MC13XXX_IRQMASK1_CLKM MC13XXX_IRQSTAT1_CLKI
124 #define MC13783_IRQMASK1_SEMAFM MC13783_IRQSTAT1_SEMAFI
125 #define MC13783_IRQMASK1_MC2BM MC13783_IRQSTAT1_MC2BI
126 #define MC13783_IRQMASK1_HSDETM MC13783_IRQSTAT1_HSDETI
127 #define MC13783_IRQMASK1_HSLM MC13783_IRQSTAT1_HSLI
128 #define MC13783_IRQMASK1_ALSPTHM MC13783_IRQSTAT1_ALSPTHI
129 #define MC13783_IRQMASK1_AHSSHORTM MC13783_IRQSTAT1_AHSSHORTI
131 #define MC13XXX_REVISION 7
132 #define MC13XXX_REVISION_REVMETAL (0x07 << 0)
133 #define MC13XXX_REVISION_REVFULL (0x03 << 3)
134 #define MC13XXX_REVISION_ICID (0x07 << 6)
135 #define MC13XXX_REVISION_FIN (0x03 << 9)
136 #define MC13XXX_REVISION_FAB (0x03 << 11)
137 #define MC13XXX_REVISION_ICIDCODE (0x3f << 13)
139 #define MC13783_ADC1 44
140 #define MC13783_ADC1_ADEN (1 << 0)
141 #define MC13783_ADC1_RAND (1 << 1)
142 #define MC13783_ADC1_ADSEL (1 << 3)
143 #define MC13783_ADC1_ASC (1 << 20)
144 #define MC13783_ADC1_ADTRIGIGN (1 << 21)
146 #define MC13783_ADC2 45
148 #define MC13XXX_NUMREGS 0x3f
150 void mc13xxx_lock(struct mc13xxx
*mc13xxx
)
152 if (!mutex_trylock(&mc13xxx
->lock
)) {
153 dev_dbg(&mc13xxx
->spidev
->dev
, "wait for %s from %pf\n",
154 __func__
, __builtin_return_address(0));
156 mutex_lock(&mc13xxx
->lock
);
158 dev_dbg(&mc13xxx
->spidev
->dev
, "%s from %pf\n",
159 __func__
, __builtin_return_address(0));
161 EXPORT_SYMBOL(mc13xxx_lock
);
163 void mc13xxx_unlock(struct mc13xxx
*mc13xxx
)
165 dev_dbg(&mc13xxx
->spidev
->dev
, "%s from %pf\n",
166 __func__
, __builtin_return_address(0));
167 mutex_unlock(&mc13xxx
->lock
);
169 EXPORT_SYMBOL(mc13xxx_unlock
);
171 #define MC13XXX_REGOFFSET_SHIFT 25
172 int mc13xxx_reg_read(struct mc13xxx
*mc13xxx
, unsigned int offset
, u32
*val
)
174 struct spi_transfer t
;
175 struct spi_message m
;
178 BUG_ON(!mutex_is_locked(&mc13xxx
->lock
));
180 if (offset
> MC13XXX_NUMREGS
)
183 *val
= offset
<< MC13XXX_REGOFFSET_SHIFT
;
185 memset(&t
, 0, sizeof(t
));
191 spi_message_init(&m
);
192 spi_message_add_tail(&t
, &m
);
194 ret
= spi_sync(mc13xxx
->spidev
, &m
);
196 /* error in message.status implies error return from spi_sync */
197 BUG_ON(!ret
&& m
.status
);
204 dev_vdbg(&mc13xxx
->spidev
->dev
, "[0x%02x] -> 0x%06x\n", offset
, *val
);
208 EXPORT_SYMBOL(mc13xxx_reg_read
);
210 int mc13xxx_reg_write(struct mc13xxx
*mc13xxx
, unsigned int offset
, u32 val
)
213 struct spi_transfer t
;
214 struct spi_message m
;
217 BUG_ON(!mutex_is_locked(&mc13xxx
->lock
));
219 dev_vdbg(&mc13xxx
->spidev
->dev
, "[0x%02x] <- 0x%06x\n", offset
, val
);
221 if (offset
> MC13XXX_NUMREGS
|| val
> 0xffffff)
224 buf
= 1 << 31 | offset
<< MC13XXX_REGOFFSET_SHIFT
| val
;
226 memset(&t
, 0, sizeof(t
));
232 spi_message_init(&m
);
233 spi_message_add_tail(&t
, &m
);
235 ret
= spi_sync(mc13xxx
->spidev
, &m
);
237 BUG_ON(!ret
&& m
.status
);
244 EXPORT_SYMBOL(mc13xxx_reg_write
);
246 int mc13xxx_reg_rmw(struct mc13xxx
*mc13xxx
, unsigned int offset
,
254 ret
= mc13xxx_reg_read(mc13xxx
, offset
, &valread
);
258 valread
= (valread
& ~mask
) | val
;
260 return mc13xxx_reg_write(mc13xxx
, offset
, valread
);
262 EXPORT_SYMBOL(mc13xxx_reg_rmw
);
264 int mc13xxx_irq_mask(struct mc13xxx
*mc13xxx
, int irq
)
267 unsigned int offmask
= irq
< 24 ? MC13XXX_IRQMASK0
: MC13XXX_IRQMASK1
;
268 u32 irqbit
= 1 << (irq
< 24 ? irq
: irq
- 24);
271 if (irq
< 0 || irq
>= MC13XXX_NUM_IRQ
)
274 ret
= mc13xxx_reg_read(mc13xxx
, offmask
, &mask
);
282 return mc13xxx_reg_write(mc13xxx
, offmask
, mask
| irqbit
);
284 EXPORT_SYMBOL(mc13xxx_irq_mask
);
286 int mc13xxx_irq_unmask(struct mc13xxx
*mc13xxx
, int irq
)
289 unsigned int offmask
= irq
< 24 ? MC13XXX_IRQMASK0
: MC13XXX_IRQMASK1
;
290 u32 irqbit
= 1 << (irq
< 24 ? irq
: irq
- 24);
293 if (irq
< 0 || irq
>= MC13XXX_NUM_IRQ
)
296 ret
= mc13xxx_reg_read(mc13xxx
, offmask
, &mask
);
300 if (!(mask
& irqbit
))
301 /* already unmasked */
304 return mc13xxx_reg_write(mc13xxx
, offmask
, mask
& ~irqbit
);
306 EXPORT_SYMBOL(mc13xxx_irq_unmask
);
308 int mc13xxx_irq_status(struct mc13xxx
*mc13xxx
, int irq
,
309 int *enabled
, int *pending
)
312 unsigned int offmask
= irq
< 24 ? MC13XXX_IRQMASK0
: MC13XXX_IRQMASK1
;
313 unsigned int offstat
= irq
< 24 ? MC13XXX_IRQSTAT0
: MC13XXX_IRQSTAT1
;
314 u32 irqbit
= 1 << (irq
< 24 ? irq
: irq
- 24);
316 if (irq
< 0 || irq
>= MC13XXX_NUM_IRQ
)
322 ret
= mc13xxx_reg_read(mc13xxx
, offmask
, &mask
);
326 *enabled
= mask
& irqbit
;
332 ret
= mc13xxx_reg_read(mc13xxx
, offstat
, &stat
);
336 *pending
= stat
& irqbit
;
341 EXPORT_SYMBOL(mc13xxx_irq_status
);
343 int mc13xxx_irq_ack(struct mc13xxx
*mc13xxx
, int irq
)
345 unsigned int offstat
= irq
< 24 ? MC13XXX_IRQSTAT0
: MC13XXX_IRQSTAT1
;
346 unsigned int val
= 1 << (irq
< 24 ? irq
: irq
- 24);
348 BUG_ON(irq
< 0 || irq
>= MC13XXX_NUM_IRQ
);
350 return mc13xxx_reg_write(mc13xxx
, offstat
, val
);
352 EXPORT_SYMBOL(mc13xxx_irq_ack
);
354 int mc13xxx_irq_request_nounmask(struct mc13xxx
*mc13xxx
, int irq
,
355 irq_handler_t handler
, const char *name
, void *dev
)
357 BUG_ON(!mutex_is_locked(&mc13xxx
->lock
));
360 if (irq
< 0 || irq
>= MC13XXX_NUM_IRQ
)
363 if (mc13xxx
->irqhandler
[irq
])
366 mc13xxx
->irqhandler
[irq
] = handler
;
367 mc13xxx
->irqdata
[irq
] = dev
;
371 EXPORT_SYMBOL(mc13xxx_irq_request_nounmask
);
373 int mc13xxx_irq_request(struct mc13xxx
*mc13xxx
, int irq
,
374 irq_handler_t handler
, const char *name
, void *dev
)
378 ret
= mc13xxx_irq_request_nounmask(mc13xxx
, irq
, handler
, name
, dev
);
382 ret
= mc13xxx_irq_unmask(mc13xxx
, irq
);
384 mc13xxx
->irqhandler
[irq
] = NULL
;
385 mc13xxx
->irqdata
[irq
] = NULL
;
391 EXPORT_SYMBOL(mc13xxx_irq_request
);
393 int mc13xxx_irq_free(struct mc13xxx
*mc13xxx
, int irq
, void *dev
)
396 BUG_ON(!mutex_is_locked(&mc13xxx
->lock
));
398 if (irq
< 0 || irq
>= MC13XXX_NUM_IRQ
|| !mc13xxx
->irqhandler
[irq
] ||
399 mc13xxx
->irqdata
[irq
] != dev
)
402 ret
= mc13xxx_irq_mask(mc13xxx
, irq
);
406 mc13xxx
->irqhandler
[irq
] = NULL
;
407 mc13xxx
->irqdata
[irq
] = NULL
;
411 EXPORT_SYMBOL(mc13xxx_irq_free
);
413 static inline irqreturn_t
mc13xxx_irqhandler(struct mc13xxx
*mc13xxx
, int irq
)
415 return mc13xxx
->irqhandler
[irq
](irq
, mc13xxx
->irqdata
[irq
]);
419 * returns: number of handled irqs or negative error
420 * locking: holds mc13xxx->lock
422 static int mc13xxx_irq_handle(struct mc13xxx
*mc13xxx
,
423 unsigned int offstat
, unsigned int offmask
, int baseirq
)
426 int ret
= mc13xxx_reg_read(mc13xxx
, offstat
, &stat
);
432 ret
= mc13xxx_reg_read(mc13xxx
, offmask
, &mask
);
436 while (stat
& ~mask
) {
437 int irq
= __ffs(stat
& ~mask
);
441 if (likely(mc13xxx
->irqhandler
[baseirq
+ irq
])) {
444 handled
= mc13xxx_irqhandler(mc13xxx
, baseirq
+ irq
);
445 if (handled
== IRQ_HANDLED
)
448 dev_err(&mc13xxx
->spidev
->dev
,
449 "BUG: irq %u but no handler\n",
454 ret
= mc13xxx_reg_write(mc13xxx
, offmask
, mask
);
461 static irqreturn_t
mc13xxx_irq_thread(int irq
, void *data
)
463 struct mc13xxx
*mc13xxx
= data
;
467 mc13xxx_lock(mc13xxx
);
469 ret
= mc13xxx_irq_handle(mc13xxx
, MC13XXX_IRQSTAT0
,
470 MC13XXX_IRQMASK0
, 0);
474 ret
= mc13xxx_irq_handle(mc13xxx
, MC13XXX_IRQSTAT1
,
475 MC13XXX_IRQMASK1
, 24);
479 mc13xxx_unlock(mc13xxx
);
481 return IRQ_RETVAL(handled
);
490 const char *mc13xxx_chipname
[] = {
491 [MC13XXX_ID_MC13783
] = "mc13783",
492 [MC13XXX_ID_MC13892
] = "mc13892",
495 #define maskval(reg, mask) (((reg) & (mask)) >> __ffs(mask))
496 static int mc13xxx_identify(struct mc13xxx
*mc13xxx
, enum mc13xxx_id
*id
)
503 ret
= mc13xxx_reg_read(mc13xxx
, 46, &icid
);
507 icid
= (icid
>> 6) & 0x7;
511 *id
= MC13XXX_ID_MC13783
;
515 *id
= MC13XXX_ID_MC13892
;
519 *id
= MC13XXX_ID_INVALID
;
523 if (*id
== MC13XXX_ID_MC13783
|| *id
== MC13XXX_ID_MC13892
) {
524 ret
= mc13xxx_reg_read(mc13xxx
, MC13XXX_REVISION
, &revision
);
528 dev_info(&mc13xxx
->spidev
->dev
, "%s: rev: %d.%d, "
529 "fin: %d, fab: %d, icid: %d/%d\n",
530 mc13xxx_chipname
[*id
],
531 maskval(revision
, MC13XXX_REVISION_REVFULL
),
532 maskval(revision
, MC13XXX_REVISION_REVMETAL
),
533 maskval(revision
, MC13XXX_REVISION_FIN
),
534 maskval(revision
, MC13XXX_REVISION_FAB
),
535 maskval(revision
, MC13XXX_REVISION_ICID
),
536 maskval(revision
, MC13XXX_REVISION_ICIDCODE
));
539 if (*id
!= MC13XXX_ID_INVALID
) {
540 const struct spi_device_id
*devid
=
541 spi_get_device_id(mc13xxx
->spidev
);
542 if (!devid
|| devid
->driver_data
!= *id
)
543 dev_warn(&mc13xxx
->spidev
->dev
, "device id doesn't "
544 "match auto detection!\n");
550 static const char *mc13xxx_get_chipname(struct mc13xxx
*mc13xxx
)
552 const struct spi_device_id
*devid
=
553 spi_get_device_id(mc13xxx
->spidev
);
558 return mc13xxx_chipname
[devid
->driver_data
];
561 #include <linux/mfd/mc13783.h>
563 int mc13xxx_get_flags(struct mc13xxx
*mc13xxx
)
565 struct mc13xxx_platform_data
*pdata
=
566 dev_get_platdata(&mc13xxx
->spidev
->dev
);
570 EXPORT_SYMBOL(mc13xxx_get_flags
);
572 #define MC13783_ADC1_CHAN0_SHIFT 5
573 #define MC13783_ADC1_CHAN1_SHIFT 8
575 struct mc13xxx_adcdone_data
{
576 struct mc13xxx
*mc13xxx
;
577 struct completion done
;
580 static irqreturn_t
mc13783_handler_adcdone(int irq
, void *data
)
582 struct mc13xxx_adcdone_data
*adcdone_data
= data
;
584 mc13xxx_irq_ack(adcdone_data
->mc13xxx
, irq
);
586 complete_all(&adcdone_data
->done
);
591 #define MC13783_ADC_WORKING (1 << 0)
593 int mc13783_adc_do_conversion(struct mc13783
*mc13783
, unsigned int mode
,
594 unsigned int channel
, unsigned int *sample
)
596 struct mc13xxx
*mc13xxx
= &mc13783
->mc13xxx
;
597 u32 adc0
, adc1
, old_adc0
;
599 struct mc13xxx_adcdone_data adcdone_data
= {
602 init_completion(&adcdone_data
.done
);
604 dev_dbg(&mc13xxx
->spidev
->dev
, "%s\n", __func__
);
606 mc13xxx_lock(mc13xxx
);
608 if (mc13783
->adcflags
& MC13783_ADC_WORKING
) {
613 mc13783
->adcflags
|= MC13783_ADC_WORKING
;
615 mc13xxx_reg_read(mc13xxx
, MC13783_ADC0
, &old_adc0
);
617 adc0
= MC13783_ADC0_ADINC1
| MC13783_ADC0_ADINC2
;
618 adc1
= MC13783_ADC1_ADEN
| MC13783_ADC1_ADTRIGIGN
| MC13783_ADC1_ASC
;
621 adc1
|= MC13783_ADC1_ADSEL
;
624 case MC13783_ADC_MODE_TS
:
625 adc0
|= MC13783_ADC0_ADREFEN
| MC13783_ADC0_TSMOD0
|
627 adc1
|= 4 << MC13783_ADC1_CHAN1_SHIFT
;
630 case MC13783_ADC_MODE_SINGLE_CHAN
:
631 adc0
|= old_adc0
& MC13783_ADC0_TSMOD_MASK
;
632 adc1
|= (channel
& 0x7) << MC13783_ADC1_CHAN0_SHIFT
;
633 adc1
|= MC13783_ADC1_RAND
;
636 case MC13783_ADC_MODE_MULT_CHAN
:
637 adc0
|= old_adc0
& MC13783_ADC0_TSMOD_MASK
;
638 adc1
|= 4 << MC13783_ADC1_CHAN1_SHIFT
;
642 mc13783_unlock(mc13783
);
646 dev_dbg(&mc13783
->mc13xxx
.spidev
->dev
, "%s: request irq\n", __func__
);
647 mc13xxx_irq_request(mc13xxx
, MC13783_IRQ_ADCDONE
,
648 mc13783_handler_adcdone
, __func__
, &adcdone_data
);
649 mc13xxx_irq_ack(mc13xxx
, MC13783_IRQ_ADCDONE
);
651 mc13xxx_reg_write(mc13xxx
, MC13783_ADC0
, adc0
);
652 mc13xxx_reg_write(mc13xxx
, MC13783_ADC1
, adc1
);
654 mc13xxx_unlock(mc13xxx
);
656 ret
= wait_for_completion_interruptible_timeout(&adcdone_data
.done
, HZ
);
661 mc13xxx_lock(mc13xxx
);
663 mc13xxx_irq_free(mc13xxx
, MC13783_IRQ_ADCDONE
, &adcdone_data
);
666 for (i
= 0; i
< 4; ++i
) {
667 ret
= mc13xxx_reg_read(mc13xxx
,
668 MC13783_ADC2
, &sample
[i
]);
673 if (mode
== MC13783_ADC_MODE_TS
)
675 mc13xxx_reg_write(mc13xxx
, MC13783_ADC0
, old_adc0
);
677 mc13783
->adcflags
&= ~MC13783_ADC_WORKING
;
679 mc13xxx_unlock(mc13xxx
);
683 EXPORT_SYMBOL_GPL(mc13783_adc_do_conversion
);
685 static int mc13xxx_add_subdevice_pdata(struct mc13xxx
*mc13xxx
,
686 const char *format
, void *pdata
, size_t pdata_size
)
689 const char *name
= mc13xxx_get_chipname(mc13xxx
);
691 struct mfd_cell cell
= {
692 .platform_data
= pdata
,
693 .data_size
= pdata_size
,
696 /* there is no asnprintf in the kernel :-( */
697 if (snprintf(buf
, sizeof(buf
), format
, name
) > sizeof(buf
))
700 cell
.name
= kmemdup(buf
, strlen(buf
) + 1, GFP_KERNEL
);
704 return mfd_add_devices(&mc13xxx
->spidev
->dev
, -1, &cell
, 1, NULL
, 0);
707 static int mc13xxx_add_subdevice(struct mc13xxx
*mc13xxx
, const char *format
)
709 return mc13xxx_add_subdevice_pdata(mc13xxx
, format
, NULL
, 0);
712 static int mc13xxx_probe(struct spi_device
*spi
)
714 struct mc13xxx
*mc13xxx
;
715 struct mc13xxx_platform_data
*pdata
= dev_get_platdata(&spi
->dev
);
719 mc13xxx
= kzalloc(sizeof(*mc13xxx
), GFP_KERNEL
);
723 dev_set_drvdata(&spi
->dev
, mc13xxx
);
724 spi
->mode
= SPI_MODE_0
| SPI_CS_HIGH
;
725 spi
->bits_per_word
= 32;
728 mc13xxx
->spidev
= spi
;
730 mutex_init(&mc13xxx
->lock
);
731 mc13xxx_lock(mc13xxx
);
733 ret
= mc13xxx_identify(mc13xxx
, &id
);
734 if (ret
|| id
== MC13XXX_ID_INVALID
)
738 ret
= mc13xxx_reg_write(mc13xxx
, MC13XXX_IRQMASK0
, 0x00ffffff);
742 ret
= mc13xxx_reg_write(mc13xxx
, MC13XXX_IRQMASK1
, 0x00ffffff);
746 ret
= request_threaded_irq(spi
->irq
, NULL
, mc13xxx_irq_thread
,
747 IRQF_ONESHOT
| IRQF_TRIGGER_HIGH
, "mc13xxx", mc13xxx
);
752 mutex_unlock(&mc13xxx
->lock
);
753 dev_set_drvdata(&spi
->dev
, NULL
);
758 mc13xxx_unlock(mc13xxx
);
760 if (pdata
->flags
& MC13XXX_USE_ADC
)
761 mc13xxx_add_subdevice(mc13xxx
, "%s-adc");
763 if (pdata
->flags
& MC13XXX_USE_CODEC
)
764 mc13xxx_add_subdevice(mc13xxx
, "%s-codec");
766 if (pdata
->flags
& MC13XXX_USE_REGULATOR
) {
767 struct mc13xxx_regulator_platform_data regulator_pdata
= {
768 .num_regulators
= pdata
->num_regulators
,
769 .regulators
= pdata
->regulators
,
772 mc13xxx_add_subdevice_pdata(mc13xxx
, "%s-regulator",
773 ®ulator_pdata
, sizeof(regulator_pdata
));
776 if (pdata
->flags
& MC13XXX_USE_RTC
)
777 mc13xxx_add_subdevice(mc13xxx
, "%s-rtc");
779 if (pdata
->flags
& MC13XXX_USE_TOUCHSCREEN
)
780 mc13xxx_add_subdevice(mc13xxx
, "%s-ts");
782 if (pdata
->flags
& MC13XXX_USE_LED
) {
783 mc13xxx_add_subdevice_pdata(mc13xxx
, "%s-led",
784 pdata
->leds
, sizeof(*pdata
->leds
));
790 static int __devexit
mc13xxx_remove(struct spi_device
*spi
)
792 struct mc13xxx
*mc13xxx
= dev_get_drvdata(&spi
->dev
);
794 free_irq(mc13xxx
->spidev
->irq
, mc13xxx
);
796 mfd_remove_devices(&spi
->dev
);
803 static const struct spi_device_id mc13xxx_device_id
[] = {
806 .driver_data
= MC13XXX_ID_MC13783
,
809 .driver_data
= MC13XXX_ID_MC13892
,
815 static struct spi_driver mc13xxx_driver
= {
816 .id_table
= mc13xxx_device_id
,
819 .bus
= &spi_bus_type
,
820 .owner
= THIS_MODULE
,
822 .probe
= mc13xxx_probe
,
823 .remove
= __devexit_p(mc13xxx_remove
),
826 static int __init
mc13xxx_init(void)
828 return spi_register_driver(&mc13xxx_driver
);
830 subsys_initcall(mc13xxx_init
);
832 static void __exit
mc13xxx_exit(void)
834 spi_unregister_driver(&mc13xxx_driver
);
836 module_exit(mc13xxx_exit
);
838 MODULE_DESCRIPTION("Core driver for Freescale MC13XXX PMIC");
839 MODULE_AUTHOR("Uwe Kleine-Koenig <u.kleine-koenig@pengutronix.de>");
840 MODULE_LICENSE("GPL v2");