spi-topcliff-pch: supports a spi mode setup and bit order setup by IO control
[zen-stable.git] / drivers / mfd / mc13xxx-core.c
blob7122386b4e3cf1830dbb3cab2f552efefeb43834
1 /*
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>
21 #include <linux/of.h>
22 #include <linux/of_device.h>
23 #include <linux/of_gpio.h>
25 struct mc13xxx {
26 struct spi_device *spidev;
27 struct mutex lock;
28 int irq;
29 int flags;
31 irq_handler_t irqhandler[MC13XXX_NUM_IRQ];
32 void *irqdata[MC13XXX_NUM_IRQ];
34 int adcflags;
37 #define MC13XXX_IRQSTAT0 0
38 #define MC13XXX_IRQSTAT0_ADCDONEI (1 << 0)
39 #define MC13XXX_IRQSTAT0_ADCBISDONEI (1 << 1)
40 #define MC13XXX_IRQSTAT0_TSI (1 << 2)
41 #define MC13783_IRQSTAT0_WHIGHI (1 << 3)
42 #define MC13783_IRQSTAT0_WLOWI (1 << 4)
43 #define MC13XXX_IRQSTAT0_CHGDETI (1 << 6)
44 #define MC13783_IRQSTAT0_CHGOVI (1 << 7)
45 #define MC13XXX_IRQSTAT0_CHGREVI (1 << 8)
46 #define MC13XXX_IRQSTAT0_CHGSHORTI (1 << 9)
47 #define MC13XXX_IRQSTAT0_CCCVI (1 << 10)
48 #define MC13XXX_IRQSTAT0_CHGCURRI (1 << 11)
49 #define MC13XXX_IRQSTAT0_BPONI (1 << 12)
50 #define MC13XXX_IRQSTAT0_LOBATLI (1 << 13)
51 #define MC13XXX_IRQSTAT0_LOBATHI (1 << 14)
52 #define MC13783_IRQSTAT0_UDPI (1 << 15)
53 #define MC13783_IRQSTAT0_USBI (1 << 16)
54 #define MC13783_IRQSTAT0_IDI (1 << 19)
55 #define MC13783_IRQSTAT0_SE1I (1 << 21)
56 #define MC13783_IRQSTAT0_CKDETI (1 << 22)
57 #define MC13783_IRQSTAT0_UDMI (1 << 23)
59 #define MC13XXX_IRQMASK0 1
60 #define MC13XXX_IRQMASK0_ADCDONEM MC13XXX_IRQSTAT0_ADCDONEI
61 #define MC13XXX_IRQMASK0_ADCBISDONEM MC13XXX_IRQSTAT0_ADCBISDONEI
62 #define MC13XXX_IRQMASK0_TSM MC13XXX_IRQSTAT0_TSI
63 #define MC13783_IRQMASK0_WHIGHM MC13783_IRQSTAT0_WHIGHI
64 #define MC13783_IRQMASK0_WLOWM MC13783_IRQSTAT0_WLOWI
65 #define MC13XXX_IRQMASK0_CHGDETM MC13XXX_IRQSTAT0_CHGDETI
66 #define MC13783_IRQMASK0_CHGOVM MC13783_IRQSTAT0_CHGOVI
67 #define MC13XXX_IRQMASK0_CHGREVM MC13XXX_IRQSTAT0_CHGREVI
68 #define MC13XXX_IRQMASK0_CHGSHORTM MC13XXX_IRQSTAT0_CHGSHORTI
69 #define MC13XXX_IRQMASK0_CCCVM MC13XXX_IRQSTAT0_CCCVI
70 #define MC13XXX_IRQMASK0_CHGCURRM MC13XXX_IRQSTAT0_CHGCURRI
71 #define MC13XXX_IRQMASK0_BPONM MC13XXX_IRQSTAT0_BPONI
72 #define MC13XXX_IRQMASK0_LOBATLM MC13XXX_IRQSTAT0_LOBATLI
73 #define MC13XXX_IRQMASK0_LOBATHM MC13XXX_IRQSTAT0_LOBATHI
74 #define MC13783_IRQMASK0_UDPM MC13783_IRQSTAT0_UDPI
75 #define MC13783_IRQMASK0_USBM MC13783_IRQSTAT0_USBI
76 #define MC13783_IRQMASK0_IDM MC13783_IRQSTAT0_IDI
77 #define MC13783_IRQMASK0_SE1M MC13783_IRQSTAT0_SE1I
78 #define MC13783_IRQMASK0_CKDETM MC13783_IRQSTAT0_CKDETI
79 #define MC13783_IRQMASK0_UDMM MC13783_IRQSTAT0_UDMI
81 #define MC13XXX_IRQSTAT1 3
82 #define MC13XXX_IRQSTAT1_1HZI (1 << 0)
83 #define MC13XXX_IRQSTAT1_TODAI (1 << 1)
84 #define MC13783_IRQSTAT1_ONOFD1I (1 << 3)
85 #define MC13783_IRQSTAT1_ONOFD2I (1 << 4)
86 #define MC13783_IRQSTAT1_ONOFD3I (1 << 5)
87 #define MC13XXX_IRQSTAT1_SYSRSTI (1 << 6)
88 #define MC13XXX_IRQSTAT1_RTCRSTI (1 << 7)
89 #define MC13XXX_IRQSTAT1_PCI (1 << 8)
90 #define MC13XXX_IRQSTAT1_WARMI (1 << 9)
91 #define MC13XXX_IRQSTAT1_MEMHLDI (1 << 10)
92 #define MC13783_IRQSTAT1_PWRRDYI (1 << 11)
93 #define MC13XXX_IRQSTAT1_THWARNLI (1 << 12)
94 #define MC13XXX_IRQSTAT1_THWARNHI (1 << 13)
95 #define MC13XXX_IRQSTAT1_CLKI (1 << 14)
96 #define MC13783_IRQSTAT1_SEMAFI (1 << 15)
97 #define MC13783_IRQSTAT1_MC2BI (1 << 17)
98 #define MC13783_IRQSTAT1_HSDETI (1 << 18)
99 #define MC13783_IRQSTAT1_HSLI (1 << 19)
100 #define MC13783_IRQSTAT1_ALSPTHI (1 << 20)
101 #define MC13783_IRQSTAT1_AHSSHORTI (1 << 21)
103 #define MC13XXX_IRQMASK1 4
104 #define MC13XXX_IRQMASK1_1HZM MC13XXX_IRQSTAT1_1HZI
105 #define MC13XXX_IRQMASK1_TODAM MC13XXX_IRQSTAT1_TODAI
106 #define MC13783_IRQMASK1_ONOFD1M MC13783_IRQSTAT1_ONOFD1I
107 #define MC13783_IRQMASK1_ONOFD2M MC13783_IRQSTAT1_ONOFD2I
108 #define MC13783_IRQMASK1_ONOFD3M MC13783_IRQSTAT1_ONOFD3I
109 #define MC13XXX_IRQMASK1_SYSRSTM MC13XXX_IRQSTAT1_SYSRSTI
110 #define MC13XXX_IRQMASK1_RTCRSTM MC13XXX_IRQSTAT1_RTCRSTI
111 #define MC13XXX_IRQMASK1_PCM MC13XXX_IRQSTAT1_PCI
112 #define MC13XXX_IRQMASK1_WARMM MC13XXX_IRQSTAT1_WARMI
113 #define MC13XXX_IRQMASK1_MEMHLDM MC13XXX_IRQSTAT1_MEMHLDI
114 #define MC13783_IRQMASK1_PWRRDYM MC13783_IRQSTAT1_PWRRDYI
115 #define MC13XXX_IRQMASK1_THWARNLM MC13XXX_IRQSTAT1_THWARNLI
116 #define MC13XXX_IRQMASK1_THWARNHM MC13XXX_IRQSTAT1_THWARNHI
117 #define MC13XXX_IRQMASK1_CLKM MC13XXX_IRQSTAT1_CLKI
118 #define MC13783_IRQMASK1_SEMAFM MC13783_IRQSTAT1_SEMAFI
119 #define MC13783_IRQMASK1_MC2BM MC13783_IRQSTAT1_MC2BI
120 #define MC13783_IRQMASK1_HSDETM MC13783_IRQSTAT1_HSDETI
121 #define MC13783_IRQMASK1_HSLM MC13783_IRQSTAT1_HSLI
122 #define MC13783_IRQMASK1_ALSPTHM MC13783_IRQSTAT1_ALSPTHI
123 #define MC13783_IRQMASK1_AHSSHORTM MC13783_IRQSTAT1_AHSSHORTI
125 #define MC13XXX_REVISION 7
126 #define MC13XXX_REVISION_REVMETAL (0x07 << 0)
127 #define MC13XXX_REVISION_REVFULL (0x03 << 3)
128 #define MC13XXX_REVISION_ICID (0x07 << 6)
129 #define MC13XXX_REVISION_FIN (0x03 << 9)
130 #define MC13XXX_REVISION_FAB (0x03 << 11)
131 #define MC13XXX_REVISION_ICIDCODE (0x3f << 13)
133 #define MC13XXX_ADC1 44
134 #define MC13XXX_ADC1_ADEN (1 << 0)
135 #define MC13XXX_ADC1_RAND (1 << 1)
136 #define MC13XXX_ADC1_ADSEL (1 << 3)
137 #define MC13XXX_ADC1_ASC (1 << 20)
138 #define MC13XXX_ADC1_ADTRIGIGN (1 << 21)
140 #define MC13XXX_ADC2 45
142 #define MC13XXX_NUMREGS 0x3f
144 void mc13xxx_lock(struct mc13xxx *mc13xxx)
146 if (!mutex_trylock(&mc13xxx->lock)) {
147 dev_dbg(&mc13xxx->spidev->dev, "wait for %s from %pf\n",
148 __func__, __builtin_return_address(0));
150 mutex_lock(&mc13xxx->lock);
152 dev_dbg(&mc13xxx->spidev->dev, "%s from %pf\n",
153 __func__, __builtin_return_address(0));
155 EXPORT_SYMBOL(mc13xxx_lock);
157 void mc13xxx_unlock(struct mc13xxx *mc13xxx)
159 dev_dbg(&mc13xxx->spidev->dev, "%s from %pf\n",
160 __func__, __builtin_return_address(0));
161 mutex_unlock(&mc13xxx->lock);
163 EXPORT_SYMBOL(mc13xxx_unlock);
165 #define MC13XXX_REGOFFSET_SHIFT 25
166 int mc13xxx_reg_read(struct mc13xxx *mc13xxx, unsigned int offset, u32 *val)
168 struct spi_transfer t;
169 struct spi_message m;
170 int ret;
172 BUG_ON(!mutex_is_locked(&mc13xxx->lock));
174 if (offset > MC13XXX_NUMREGS)
175 return -EINVAL;
177 *val = offset << MC13XXX_REGOFFSET_SHIFT;
179 memset(&t, 0, sizeof(t));
181 t.tx_buf = val;
182 t.rx_buf = val;
183 t.len = sizeof(u32);
185 spi_message_init(&m);
186 spi_message_add_tail(&t, &m);
188 ret = spi_sync(mc13xxx->spidev, &m);
190 /* error in message.status implies error return from spi_sync */
191 BUG_ON(!ret && m.status);
193 if (ret)
194 return ret;
196 *val &= 0xffffff;
198 dev_vdbg(&mc13xxx->spidev->dev, "[0x%02x] -> 0x%06x\n", offset, *val);
200 return 0;
202 EXPORT_SYMBOL(mc13xxx_reg_read);
204 int mc13xxx_reg_write(struct mc13xxx *mc13xxx, unsigned int offset, u32 val)
206 u32 buf;
207 struct spi_transfer t;
208 struct spi_message m;
209 int ret;
211 BUG_ON(!mutex_is_locked(&mc13xxx->lock));
213 dev_vdbg(&mc13xxx->spidev->dev, "[0x%02x] <- 0x%06x\n", offset, val);
215 if (offset > MC13XXX_NUMREGS || val > 0xffffff)
216 return -EINVAL;
218 buf = 1 << 31 | offset << MC13XXX_REGOFFSET_SHIFT | val;
220 memset(&t, 0, sizeof(t));
222 t.tx_buf = &buf;
223 t.rx_buf = &buf;
224 t.len = sizeof(u32);
226 spi_message_init(&m);
227 spi_message_add_tail(&t, &m);
229 ret = spi_sync(mc13xxx->spidev, &m);
231 BUG_ON(!ret && m.status);
233 if (ret)
234 return ret;
236 return 0;
238 EXPORT_SYMBOL(mc13xxx_reg_write);
240 int mc13xxx_reg_rmw(struct mc13xxx *mc13xxx, unsigned int offset,
241 u32 mask, u32 val)
243 int ret;
244 u32 valread;
246 BUG_ON(val & ~mask);
248 ret = mc13xxx_reg_read(mc13xxx, offset, &valread);
249 if (ret)
250 return ret;
252 valread = (valread & ~mask) | val;
254 return mc13xxx_reg_write(mc13xxx, offset, valread);
256 EXPORT_SYMBOL(mc13xxx_reg_rmw);
258 int mc13xxx_irq_mask(struct mc13xxx *mc13xxx, int irq)
260 int ret;
261 unsigned int offmask = irq < 24 ? MC13XXX_IRQMASK0 : MC13XXX_IRQMASK1;
262 u32 irqbit = 1 << (irq < 24 ? irq : irq - 24);
263 u32 mask;
265 if (irq < 0 || irq >= MC13XXX_NUM_IRQ)
266 return -EINVAL;
268 ret = mc13xxx_reg_read(mc13xxx, offmask, &mask);
269 if (ret)
270 return ret;
272 if (mask & irqbit)
273 /* already masked */
274 return 0;
276 return mc13xxx_reg_write(mc13xxx, offmask, mask | irqbit);
278 EXPORT_SYMBOL(mc13xxx_irq_mask);
280 int mc13xxx_irq_unmask(struct mc13xxx *mc13xxx, int irq)
282 int ret;
283 unsigned int offmask = irq < 24 ? MC13XXX_IRQMASK0 : MC13XXX_IRQMASK1;
284 u32 irqbit = 1 << (irq < 24 ? irq : irq - 24);
285 u32 mask;
287 if (irq < 0 || irq >= MC13XXX_NUM_IRQ)
288 return -EINVAL;
290 ret = mc13xxx_reg_read(mc13xxx, offmask, &mask);
291 if (ret)
292 return ret;
294 if (!(mask & irqbit))
295 /* already unmasked */
296 return 0;
298 return mc13xxx_reg_write(mc13xxx, offmask, mask & ~irqbit);
300 EXPORT_SYMBOL(mc13xxx_irq_unmask);
302 int mc13xxx_irq_status(struct mc13xxx *mc13xxx, int irq,
303 int *enabled, int *pending)
305 int ret;
306 unsigned int offmask = irq < 24 ? MC13XXX_IRQMASK0 : MC13XXX_IRQMASK1;
307 unsigned int offstat = irq < 24 ? MC13XXX_IRQSTAT0 : MC13XXX_IRQSTAT1;
308 u32 irqbit = 1 << (irq < 24 ? irq : irq - 24);
310 if (irq < 0 || irq >= MC13XXX_NUM_IRQ)
311 return -EINVAL;
313 if (enabled) {
314 u32 mask;
316 ret = mc13xxx_reg_read(mc13xxx, offmask, &mask);
317 if (ret)
318 return ret;
320 *enabled = mask & irqbit;
323 if (pending) {
324 u32 stat;
326 ret = mc13xxx_reg_read(mc13xxx, offstat, &stat);
327 if (ret)
328 return ret;
330 *pending = stat & irqbit;
333 return 0;
335 EXPORT_SYMBOL(mc13xxx_irq_status);
337 int mc13xxx_irq_ack(struct mc13xxx *mc13xxx, int irq)
339 unsigned int offstat = irq < 24 ? MC13XXX_IRQSTAT0 : MC13XXX_IRQSTAT1;
340 unsigned int val = 1 << (irq < 24 ? irq : irq - 24);
342 BUG_ON(irq < 0 || irq >= MC13XXX_NUM_IRQ);
344 return mc13xxx_reg_write(mc13xxx, offstat, val);
346 EXPORT_SYMBOL(mc13xxx_irq_ack);
348 int mc13xxx_irq_request_nounmask(struct mc13xxx *mc13xxx, int irq,
349 irq_handler_t handler, const char *name, void *dev)
351 BUG_ON(!mutex_is_locked(&mc13xxx->lock));
352 BUG_ON(!handler);
354 if (irq < 0 || irq >= MC13XXX_NUM_IRQ)
355 return -EINVAL;
357 if (mc13xxx->irqhandler[irq])
358 return -EBUSY;
360 mc13xxx->irqhandler[irq] = handler;
361 mc13xxx->irqdata[irq] = dev;
363 return 0;
365 EXPORT_SYMBOL(mc13xxx_irq_request_nounmask);
367 int mc13xxx_irq_request(struct mc13xxx *mc13xxx, int irq,
368 irq_handler_t handler, const char *name, void *dev)
370 int ret;
372 ret = mc13xxx_irq_request_nounmask(mc13xxx, irq, handler, name, dev);
373 if (ret)
374 return ret;
376 ret = mc13xxx_irq_unmask(mc13xxx, irq);
377 if (ret) {
378 mc13xxx->irqhandler[irq] = NULL;
379 mc13xxx->irqdata[irq] = NULL;
380 return ret;
383 return 0;
385 EXPORT_SYMBOL(mc13xxx_irq_request);
387 int mc13xxx_irq_free(struct mc13xxx *mc13xxx, int irq, void *dev)
389 int ret;
390 BUG_ON(!mutex_is_locked(&mc13xxx->lock));
392 if (irq < 0 || irq >= MC13XXX_NUM_IRQ || !mc13xxx->irqhandler[irq] ||
393 mc13xxx->irqdata[irq] != dev)
394 return -EINVAL;
396 ret = mc13xxx_irq_mask(mc13xxx, irq);
397 if (ret)
398 return ret;
400 mc13xxx->irqhandler[irq] = NULL;
401 mc13xxx->irqdata[irq] = NULL;
403 return 0;
405 EXPORT_SYMBOL(mc13xxx_irq_free);
407 static inline irqreturn_t mc13xxx_irqhandler(struct mc13xxx *mc13xxx, int irq)
409 return mc13xxx->irqhandler[irq](irq, mc13xxx->irqdata[irq]);
413 * returns: number of handled irqs or negative error
414 * locking: holds mc13xxx->lock
416 static int mc13xxx_irq_handle(struct mc13xxx *mc13xxx,
417 unsigned int offstat, unsigned int offmask, int baseirq)
419 u32 stat, mask;
420 int ret = mc13xxx_reg_read(mc13xxx, offstat, &stat);
421 int num_handled = 0;
423 if (ret)
424 return ret;
426 ret = mc13xxx_reg_read(mc13xxx, offmask, &mask);
427 if (ret)
428 return ret;
430 while (stat & ~mask) {
431 int irq = __ffs(stat & ~mask);
433 stat &= ~(1 << irq);
435 if (likely(mc13xxx->irqhandler[baseirq + irq])) {
436 irqreturn_t handled;
438 handled = mc13xxx_irqhandler(mc13xxx, baseirq + irq);
439 if (handled == IRQ_HANDLED)
440 num_handled++;
441 } else {
442 dev_err(&mc13xxx->spidev->dev,
443 "BUG: irq %u but no handler\n",
444 baseirq + irq);
446 mask |= 1 << irq;
448 ret = mc13xxx_reg_write(mc13xxx, offmask, mask);
452 return num_handled;
455 static irqreturn_t mc13xxx_irq_thread(int irq, void *data)
457 struct mc13xxx *mc13xxx = data;
458 irqreturn_t ret;
459 int handled = 0;
461 mc13xxx_lock(mc13xxx);
463 ret = mc13xxx_irq_handle(mc13xxx, MC13XXX_IRQSTAT0,
464 MC13XXX_IRQMASK0, 0);
465 if (ret > 0)
466 handled = 1;
468 ret = mc13xxx_irq_handle(mc13xxx, MC13XXX_IRQSTAT1,
469 MC13XXX_IRQMASK1, 24);
470 if (ret > 0)
471 handled = 1;
473 mc13xxx_unlock(mc13xxx);
475 return IRQ_RETVAL(handled);
478 enum mc13xxx_id {
479 MC13XXX_ID_MC13783,
480 MC13XXX_ID_MC13892,
481 MC13XXX_ID_INVALID,
484 static const char *mc13xxx_chipname[] = {
485 [MC13XXX_ID_MC13783] = "mc13783",
486 [MC13XXX_ID_MC13892] = "mc13892",
489 #define maskval(reg, mask) (((reg) & (mask)) >> __ffs(mask))
490 static int mc13xxx_identify(struct mc13xxx *mc13xxx, enum mc13xxx_id *id)
492 u32 icid;
493 u32 revision;
494 const char *name;
495 int ret;
497 ret = mc13xxx_reg_read(mc13xxx, 46, &icid);
498 if (ret)
499 return ret;
501 icid = (icid >> 6) & 0x7;
503 switch (icid) {
504 case 2:
505 *id = MC13XXX_ID_MC13783;
506 name = "mc13783";
507 break;
508 case 7:
509 *id = MC13XXX_ID_MC13892;
510 name = "mc13892";
511 break;
512 default:
513 *id = MC13XXX_ID_INVALID;
514 break;
517 if (*id == MC13XXX_ID_MC13783 || *id == MC13XXX_ID_MC13892) {
518 ret = mc13xxx_reg_read(mc13xxx, MC13XXX_REVISION, &revision);
519 if (ret)
520 return ret;
522 dev_info(&mc13xxx->spidev->dev, "%s: rev: %d.%d, "
523 "fin: %d, fab: %d, icid: %d/%d\n",
524 mc13xxx_chipname[*id],
525 maskval(revision, MC13XXX_REVISION_REVFULL),
526 maskval(revision, MC13XXX_REVISION_REVMETAL),
527 maskval(revision, MC13XXX_REVISION_FIN),
528 maskval(revision, MC13XXX_REVISION_FAB),
529 maskval(revision, MC13XXX_REVISION_ICID),
530 maskval(revision, MC13XXX_REVISION_ICIDCODE));
533 if (*id != MC13XXX_ID_INVALID) {
534 const struct spi_device_id *devid =
535 spi_get_device_id(mc13xxx->spidev);
536 if (!devid || devid->driver_data != *id)
537 dev_warn(&mc13xxx->spidev->dev, "device id doesn't "
538 "match auto detection!\n");
541 return 0;
544 static const char *mc13xxx_get_chipname(struct mc13xxx *mc13xxx)
546 const struct spi_device_id *devid =
547 spi_get_device_id(mc13xxx->spidev);
549 if (!devid)
550 return NULL;
552 return mc13xxx_chipname[devid->driver_data];
555 int mc13xxx_get_flags(struct mc13xxx *mc13xxx)
557 return mc13xxx->flags;
559 EXPORT_SYMBOL(mc13xxx_get_flags);
561 #define MC13XXX_ADC1_CHAN0_SHIFT 5
562 #define MC13XXX_ADC1_CHAN1_SHIFT 8
564 struct mc13xxx_adcdone_data {
565 struct mc13xxx *mc13xxx;
566 struct completion done;
569 static irqreturn_t mc13xxx_handler_adcdone(int irq, void *data)
571 struct mc13xxx_adcdone_data *adcdone_data = data;
573 mc13xxx_irq_ack(adcdone_data->mc13xxx, irq);
575 complete_all(&adcdone_data->done);
577 return IRQ_HANDLED;
580 #define MC13XXX_ADC_WORKING (1 << 0)
582 int mc13xxx_adc_do_conversion(struct mc13xxx *mc13xxx, unsigned int mode,
583 unsigned int channel, unsigned int *sample)
585 u32 adc0, adc1, old_adc0;
586 int i, ret;
587 struct mc13xxx_adcdone_data adcdone_data = {
588 .mc13xxx = mc13xxx,
590 init_completion(&adcdone_data.done);
592 dev_dbg(&mc13xxx->spidev->dev, "%s\n", __func__);
594 mc13xxx_lock(mc13xxx);
596 if (mc13xxx->adcflags & MC13XXX_ADC_WORKING) {
597 ret = -EBUSY;
598 goto out;
601 mc13xxx->adcflags |= MC13XXX_ADC_WORKING;
603 mc13xxx_reg_read(mc13xxx, MC13XXX_ADC0, &old_adc0);
605 adc0 = MC13XXX_ADC0_ADINC1 | MC13XXX_ADC0_ADINC2;
606 adc1 = MC13XXX_ADC1_ADEN | MC13XXX_ADC1_ADTRIGIGN | MC13XXX_ADC1_ASC;
608 if (channel > 7)
609 adc1 |= MC13XXX_ADC1_ADSEL;
611 switch (mode) {
612 case MC13XXX_ADC_MODE_TS:
613 adc0 |= MC13XXX_ADC0_ADREFEN | MC13XXX_ADC0_TSMOD0 |
614 MC13XXX_ADC0_TSMOD1;
615 adc1 |= 4 << MC13XXX_ADC1_CHAN1_SHIFT;
616 break;
618 case MC13XXX_ADC_MODE_SINGLE_CHAN:
619 adc0 |= old_adc0 & MC13XXX_ADC0_CONFIG_MASK;
620 adc1 |= (channel & 0x7) << MC13XXX_ADC1_CHAN0_SHIFT;
621 adc1 |= MC13XXX_ADC1_RAND;
622 break;
624 case MC13XXX_ADC_MODE_MULT_CHAN:
625 adc0 |= old_adc0 & MC13XXX_ADC0_CONFIG_MASK;
626 adc1 |= 4 << MC13XXX_ADC1_CHAN1_SHIFT;
627 break;
629 default:
630 mc13xxx_unlock(mc13xxx);
631 return -EINVAL;
634 dev_dbg(&mc13xxx->spidev->dev, "%s: request irq\n", __func__);
635 mc13xxx_irq_request(mc13xxx, MC13XXX_IRQ_ADCDONE,
636 mc13xxx_handler_adcdone, __func__, &adcdone_data);
637 mc13xxx_irq_ack(mc13xxx, MC13XXX_IRQ_ADCDONE);
639 mc13xxx_reg_write(mc13xxx, MC13XXX_ADC0, adc0);
640 mc13xxx_reg_write(mc13xxx, MC13XXX_ADC1, adc1);
642 mc13xxx_unlock(mc13xxx);
644 ret = wait_for_completion_interruptible_timeout(&adcdone_data.done, HZ);
646 if (!ret)
647 ret = -ETIMEDOUT;
649 mc13xxx_lock(mc13xxx);
651 mc13xxx_irq_free(mc13xxx, MC13XXX_IRQ_ADCDONE, &adcdone_data);
653 if (ret > 0)
654 for (i = 0; i < 4; ++i) {
655 ret = mc13xxx_reg_read(mc13xxx,
656 MC13XXX_ADC2, &sample[i]);
657 if (ret)
658 break;
661 if (mode == MC13XXX_ADC_MODE_TS)
662 /* restore TSMOD */
663 mc13xxx_reg_write(mc13xxx, MC13XXX_ADC0, old_adc0);
665 mc13xxx->adcflags &= ~MC13XXX_ADC_WORKING;
666 out:
667 mc13xxx_unlock(mc13xxx);
669 return ret;
671 EXPORT_SYMBOL_GPL(mc13xxx_adc_do_conversion);
673 static int mc13xxx_add_subdevice_pdata(struct mc13xxx *mc13xxx,
674 const char *format, void *pdata, size_t pdata_size)
676 char buf[30];
677 const char *name = mc13xxx_get_chipname(mc13xxx);
679 struct mfd_cell cell = {
680 .platform_data = pdata,
681 .pdata_size = pdata_size,
684 /* there is no asnprintf in the kernel :-( */
685 if (snprintf(buf, sizeof(buf), format, name) > sizeof(buf))
686 return -E2BIG;
688 cell.name = kmemdup(buf, strlen(buf) + 1, GFP_KERNEL);
689 if (!cell.name)
690 return -ENOMEM;
692 return mfd_add_devices(&mc13xxx->spidev->dev, -1, &cell, 1, NULL, 0);
695 static int mc13xxx_add_subdevice(struct mc13xxx *mc13xxx, const char *format)
697 return mc13xxx_add_subdevice_pdata(mc13xxx, format, NULL, 0);
700 #ifdef CONFIG_OF
701 static int mc13xxx_probe_flags_dt(struct mc13xxx *mc13xxx)
703 struct device_node *np = mc13xxx->spidev->dev.of_node;
705 if (!np)
706 return -ENODEV;
708 if (of_get_property(np, "fsl,mc13xxx-uses-adc", NULL))
709 mc13xxx->flags |= MC13XXX_USE_ADC;
711 if (of_get_property(np, "fsl,mc13xxx-uses-codec", NULL))
712 mc13xxx->flags |= MC13XXX_USE_CODEC;
714 if (of_get_property(np, "fsl,mc13xxx-uses-rtc", NULL))
715 mc13xxx->flags |= MC13XXX_USE_RTC;
717 if (of_get_property(np, "fsl,mc13xxx-uses-touch", NULL))
718 mc13xxx->flags |= MC13XXX_USE_TOUCHSCREEN;
720 return 0;
722 #else
723 static inline int mc13xxx_probe_flags_dt(struct mc13xxx *mc13xxx)
725 return -ENODEV;
727 #endif
729 static const struct spi_device_id mc13xxx_device_id[] = {
731 .name = "mc13783",
732 .driver_data = MC13XXX_ID_MC13783,
733 }, {
734 .name = "mc13892",
735 .driver_data = MC13XXX_ID_MC13892,
736 }, {
737 /* sentinel */
740 MODULE_DEVICE_TABLE(spi, mc13xxx_device_id);
742 static const struct of_device_id mc13xxx_dt_ids[] = {
743 { .compatible = "fsl,mc13783", .data = (void *) MC13XXX_ID_MC13783, },
744 { .compatible = "fsl,mc13892", .data = (void *) MC13XXX_ID_MC13892, },
745 { /* sentinel */ }
747 MODULE_DEVICE_TABLE(of, mc13xxx_dt_ids);
749 static int mc13xxx_probe(struct spi_device *spi)
751 const struct of_device_id *of_id;
752 struct spi_driver *sdrv = to_spi_driver(spi->dev.driver);
753 struct mc13xxx *mc13xxx;
754 struct mc13xxx_platform_data *pdata = dev_get_platdata(&spi->dev);
755 enum mc13xxx_id id;
756 int ret;
758 of_id = of_match_device(mc13xxx_dt_ids, &spi->dev);
759 if (of_id)
760 sdrv->id_table = &mc13xxx_device_id[(enum mc13xxx_id) of_id->data];
762 mc13xxx = kzalloc(sizeof(*mc13xxx), GFP_KERNEL);
763 if (!mc13xxx)
764 return -ENOMEM;
766 dev_set_drvdata(&spi->dev, mc13xxx);
767 spi->mode = SPI_MODE_0 | SPI_CS_HIGH;
768 spi->bits_per_word = 32;
769 spi_setup(spi);
771 mc13xxx->spidev = spi;
773 mutex_init(&mc13xxx->lock);
774 mc13xxx_lock(mc13xxx);
776 ret = mc13xxx_identify(mc13xxx, &id);
777 if (ret || id == MC13XXX_ID_INVALID)
778 goto err_revision;
780 /* mask all irqs */
781 ret = mc13xxx_reg_write(mc13xxx, MC13XXX_IRQMASK0, 0x00ffffff);
782 if (ret)
783 goto err_mask;
785 ret = mc13xxx_reg_write(mc13xxx, MC13XXX_IRQMASK1, 0x00ffffff);
786 if (ret)
787 goto err_mask;
789 ret = request_threaded_irq(spi->irq, NULL, mc13xxx_irq_thread,
790 IRQF_ONESHOT | IRQF_TRIGGER_HIGH, "mc13xxx", mc13xxx);
792 if (ret) {
793 err_mask:
794 err_revision:
795 mc13xxx_unlock(mc13xxx);
796 dev_set_drvdata(&spi->dev, NULL);
797 kfree(mc13xxx);
798 return ret;
801 mc13xxx_unlock(mc13xxx);
803 if (mc13xxx_probe_flags_dt(mc13xxx) < 0 && pdata)
804 mc13xxx->flags = pdata->flags;
806 if (mc13xxx->flags & MC13XXX_USE_ADC)
807 mc13xxx_add_subdevice(mc13xxx, "%s-adc");
809 if (mc13xxx->flags & MC13XXX_USE_CODEC)
810 mc13xxx_add_subdevice(mc13xxx, "%s-codec");
812 if (mc13xxx->flags & MC13XXX_USE_RTC)
813 mc13xxx_add_subdevice(mc13xxx, "%s-rtc");
815 if (mc13xxx->flags & MC13XXX_USE_TOUCHSCREEN)
816 mc13xxx_add_subdevice(mc13xxx, "%s-ts");
818 if (pdata) {
819 mc13xxx_add_subdevice_pdata(mc13xxx, "%s-regulator",
820 &pdata->regulators, sizeof(pdata->regulators));
821 mc13xxx_add_subdevice_pdata(mc13xxx, "%s-led",
822 pdata->leds, sizeof(*pdata->leds));
823 mc13xxx_add_subdevice_pdata(mc13xxx, "%s-pwrbutton",
824 pdata->buttons, sizeof(*pdata->buttons));
825 } else {
826 mc13xxx_add_subdevice(mc13xxx, "%s-regulator");
827 mc13xxx_add_subdevice(mc13xxx, "%s-led");
828 mc13xxx_add_subdevice(mc13xxx, "%s-pwrbutton");
831 return 0;
834 static int __devexit mc13xxx_remove(struct spi_device *spi)
836 struct mc13xxx *mc13xxx = dev_get_drvdata(&spi->dev);
838 free_irq(mc13xxx->spidev->irq, mc13xxx);
840 mfd_remove_devices(&spi->dev);
842 kfree(mc13xxx);
844 return 0;
847 static struct spi_driver mc13xxx_driver = {
848 .id_table = mc13xxx_device_id,
849 .driver = {
850 .name = "mc13xxx",
851 .owner = THIS_MODULE,
852 .of_match_table = mc13xxx_dt_ids,
854 .probe = mc13xxx_probe,
855 .remove = __devexit_p(mc13xxx_remove),
858 static int __init mc13xxx_init(void)
860 return spi_register_driver(&mc13xxx_driver);
862 subsys_initcall(mc13xxx_init);
864 static void __exit mc13xxx_exit(void)
866 spi_unregister_driver(&mc13xxx_driver);
868 module_exit(mc13xxx_exit);
870 MODULE_DESCRIPTION("Core driver for Freescale MC13XXX PMIC");
871 MODULE_AUTHOR("Uwe Kleine-Koenig <u.kleine-koenig@pengutronix.de>");
872 MODULE_LICENSE("GPL v2");