blob: 2a9b100c48259deed696305ada91a65faa8b4e46 [file] [log] [blame]
Uwe Kleine-König8e005932010-09-28 16:37:20 +02001/*
2 * Copyright 2009-2010 Pengutronix
3 * Uwe Kleine-Koenig <u.kleine-koenig@pengutronix.de>
4 *
5 * loosely based on an earlier driver that has
6 * Copyright 2009 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
7 *
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.
11 */
Uwe Kleine-König8e005932010-09-28 16:37:20 +020012
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>
Uwe Kleine-König8e005932010-09-28 16:37:20 +020018#include <linux/mfd/core.h>
19#include <linux/mfd/mc13xxx.h>
Shawn Guo876989d2011-12-12 18:52:57 +010020#include <linux/of.h>
21#include <linux/of_device.h>
22#include <linux/of_gpio.h>
Uwe Kleine-König8e005932010-09-28 16:37:20 +020023
Marc Reillya0c7c1d2012-04-01 16:41:38 +100024#include "mc13xxx.h"
Uwe Kleine-König8e005932010-09-28 16:37:20 +020025
Uwe Kleine-König8e005932010-09-28 16:37:20 +020026#define MC13XXX_IRQSTAT0 0
27#define MC13XXX_IRQSTAT0_ADCDONEI (1 << 0)
28#define MC13XXX_IRQSTAT0_ADCBISDONEI (1 << 1)
29#define MC13XXX_IRQSTAT0_TSI (1 << 2)
30#define MC13783_IRQSTAT0_WHIGHI (1 << 3)
31#define MC13783_IRQSTAT0_WLOWI (1 << 4)
32#define MC13XXX_IRQSTAT0_CHGDETI (1 << 6)
33#define MC13783_IRQSTAT0_CHGOVI (1 << 7)
34#define MC13XXX_IRQSTAT0_CHGREVI (1 << 8)
35#define MC13XXX_IRQSTAT0_CHGSHORTI (1 << 9)
36#define MC13XXX_IRQSTAT0_CCCVI (1 << 10)
37#define MC13XXX_IRQSTAT0_CHGCURRI (1 << 11)
38#define MC13XXX_IRQSTAT0_BPONI (1 << 12)
39#define MC13XXX_IRQSTAT0_LOBATLI (1 << 13)
40#define MC13XXX_IRQSTAT0_LOBATHI (1 << 14)
41#define MC13783_IRQSTAT0_UDPI (1 << 15)
42#define MC13783_IRQSTAT0_USBI (1 << 16)
43#define MC13783_IRQSTAT0_IDI (1 << 19)
44#define MC13783_IRQSTAT0_SE1I (1 << 21)
45#define MC13783_IRQSTAT0_CKDETI (1 << 22)
46#define MC13783_IRQSTAT0_UDMI (1 << 23)
47
48#define MC13XXX_IRQMASK0 1
49#define MC13XXX_IRQMASK0_ADCDONEM MC13XXX_IRQSTAT0_ADCDONEI
50#define MC13XXX_IRQMASK0_ADCBISDONEM MC13XXX_IRQSTAT0_ADCBISDONEI
51#define MC13XXX_IRQMASK0_TSM MC13XXX_IRQSTAT0_TSI
52#define MC13783_IRQMASK0_WHIGHM MC13783_IRQSTAT0_WHIGHI
53#define MC13783_IRQMASK0_WLOWM MC13783_IRQSTAT0_WLOWI
54#define MC13XXX_IRQMASK0_CHGDETM MC13XXX_IRQSTAT0_CHGDETI
55#define MC13783_IRQMASK0_CHGOVM MC13783_IRQSTAT0_CHGOVI
56#define MC13XXX_IRQMASK0_CHGREVM MC13XXX_IRQSTAT0_CHGREVI
57#define MC13XXX_IRQMASK0_CHGSHORTM MC13XXX_IRQSTAT0_CHGSHORTI
58#define MC13XXX_IRQMASK0_CCCVM MC13XXX_IRQSTAT0_CCCVI
59#define MC13XXX_IRQMASK0_CHGCURRM MC13XXX_IRQSTAT0_CHGCURRI
60#define MC13XXX_IRQMASK0_BPONM MC13XXX_IRQSTAT0_BPONI
61#define MC13XXX_IRQMASK0_LOBATLM MC13XXX_IRQSTAT0_LOBATLI
62#define MC13XXX_IRQMASK0_LOBATHM MC13XXX_IRQSTAT0_LOBATHI
63#define MC13783_IRQMASK0_UDPM MC13783_IRQSTAT0_UDPI
64#define MC13783_IRQMASK0_USBM MC13783_IRQSTAT0_USBI
65#define MC13783_IRQMASK0_IDM MC13783_IRQSTAT0_IDI
66#define MC13783_IRQMASK0_SE1M MC13783_IRQSTAT0_SE1I
67#define MC13783_IRQMASK0_CKDETM MC13783_IRQSTAT0_CKDETI
68#define MC13783_IRQMASK0_UDMM MC13783_IRQSTAT0_UDMI
69
70#define MC13XXX_IRQSTAT1 3
71#define MC13XXX_IRQSTAT1_1HZI (1 << 0)
72#define MC13XXX_IRQSTAT1_TODAI (1 << 1)
73#define MC13783_IRQSTAT1_ONOFD1I (1 << 3)
74#define MC13783_IRQSTAT1_ONOFD2I (1 << 4)
75#define MC13783_IRQSTAT1_ONOFD3I (1 << 5)
76#define MC13XXX_IRQSTAT1_SYSRSTI (1 << 6)
77#define MC13XXX_IRQSTAT1_RTCRSTI (1 << 7)
78#define MC13XXX_IRQSTAT1_PCI (1 << 8)
79#define MC13XXX_IRQSTAT1_WARMI (1 << 9)
80#define MC13XXX_IRQSTAT1_MEMHLDI (1 << 10)
81#define MC13783_IRQSTAT1_PWRRDYI (1 << 11)
82#define MC13XXX_IRQSTAT1_THWARNLI (1 << 12)
83#define MC13XXX_IRQSTAT1_THWARNHI (1 << 13)
84#define MC13XXX_IRQSTAT1_CLKI (1 << 14)
85#define MC13783_IRQSTAT1_SEMAFI (1 << 15)
86#define MC13783_IRQSTAT1_MC2BI (1 << 17)
87#define MC13783_IRQSTAT1_HSDETI (1 << 18)
88#define MC13783_IRQSTAT1_HSLI (1 << 19)
89#define MC13783_IRQSTAT1_ALSPTHI (1 << 20)
90#define MC13783_IRQSTAT1_AHSSHORTI (1 << 21)
91
92#define MC13XXX_IRQMASK1 4
93#define MC13XXX_IRQMASK1_1HZM MC13XXX_IRQSTAT1_1HZI
94#define MC13XXX_IRQMASK1_TODAM MC13XXX_IRQSTAT1_TODAI
95#define MC13783_IRQMASK1_ONOFD1M MC13783_IRQSTAT1_ONOFD1I
96#define MC13783_IRQMASK1_ONOFD2M MC13783_IRQSTAT1_ONOFD2I
97#define MC13783_IRQMASK1_ONOFD3M MC13783_IRQSTAT1_ONOFD3I
98#define MC13XXX_IRQMASK1_SYSRSTM MC13XXX_IRQSTAT1_SYSRSTI
99#define MC13XXX_IRQMASK1_RTCRSTM MC13XXX_IRQSTAT1_RTCRSTI
100#define MC13XXX_IRQMASK1_PCM MC13XXX_IRQSTAT1_PCI
101#define MC13XXX_IRQMASK1_WARMM MC13XXX_IRQSTAT1_WARMI
102#define MC13XXX_IRQMASK1_MEMHLDM MC13XXX_IRQSTAT1_MEMHLDI
103#define MC13783_IRQMASK1_PWRRDYM MC13783_IRQSTAT1_PWRRDYI
104#define MC13XXX_IRQMASK1_THWARNLM MC13XXX_IRQSTAT1_THWARNLI
105#define MC13XXX_IRQMASK1_THWARNHM MC13XXX_IRQSTAT1_THWARNHI
106#define MC13XXX_IRQMASK1_CLKM MC13XXX_IRQSTAT1_CLKI
107#define MC13783_IRQMASK1_SEMAFM MC13783_IRQSTAT1_SEMAFI
108#define MC13783_IRQMASK1_MC2BM MC13783_IRQSTAT1_MC2BI
109#define MC13783_IRQMASK1_HSDETM MC13783_IRQSTAT1_HSDETI
110#define MC13783_IRQMASK1_HSLM MC13783_IRQSTAT1_HSLI
111#define MC13783_IRQMASK1_ALSPTHM MC13783_IRQSTAT1_ALSPTHI
112#define MC13783_IRQMASK1_AHSSHORTM MC13783_IRQSTAT1_AHSSHORTI
113
114#define MC13XXX_REVISION 7
115#define MC13XXX_REVISION_REVMETAL (0x07 << 0)
116#define MC13XXX_REVISION_REVFULL (0x03 << 3)
117#define MC13XXX_REVISION_ICID (0x07 << 6)
118#define MC13XXX_REVISION_FIN (0x03 << 9)
119#define MC13XXX_REVISION_FAB (0x03 << 11)
120#define MC13XXX_REVISION_ICIDCODE (0x3f << 13)
121
Uwe Kleine-König0312e022012-07-12 09:57:53 +0000122#define MC34708_REVISION_REVMETAL (0x07 << 0)
123#define MC34708_REVISION_REVFULL (0x07 << 3)
124#define MC34708_REVISION_FIN (0x07 << 6)
125#define MC34708_REVISION_FAB (0x07 << 9)
126
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200127#define MC13XXX_ADC1 44
128#define MC13XXX_ADC1_ADEN (1 << 0)
129#define MC13XXX_ADC1_RAND (1 << 1)
130#define MC13XXX_ADC1_ADSEL (1 << 3)
131#define MC13XXX_ADC1_ASC (1 << 20)
132#define MC13XXX_ADC1_ADTRIGIGN (1 << 21)
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200133
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200134#define MC13XXX_ADC2 45
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200135
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200136void mc13xxx_lock(struct mc13xxx *mc13xxx)
137{
138 if (!mutex_trylock(&mc13xxx->lock)) {
Marc Reilly5006fe52012-05-01 12:26:46 +0200139 dev_dbg(mc13xxx->dev, "wait for %s from %pf\n",
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200140 __func__, __builtin_return_address(0));
141
142 mutex_lock(&mc13xxx->lock);
143 }
Marc Reilly5006fe52012-05-01 12:26:46 +0200144 dev_dbg(mc13xxx->dev, "%s from %pf\n",
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200145 __func__, __builtin_return_address(0));
146}
147EXPORT_SYMBOL(mc13xxx_lock);
148
149void mc13xxx_unlock(struct mc13xxx *mc13xxx)
150{
Marc Reilly5006fe52012-05-01 12:26:46 +0200151 dev_dbg(mc13xxx->dev, "%s from %pf\n",
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200152 __func__, __builtin_return_address(0));
153 mutex_unlock(&mc13xxx->lock);
154}
155EXPORT_SYMBOL(mc13xxx_unlock);
156
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200157int mc13xxx_reg_read(struct mc13xxx *mc13xxx, unsigned int offset, u32 *val)
158{
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200159 int ret;
160
161 BUG_ON(!mutex_is_locked(&mc13xxx->lock));
162
163 if (offset > MC13XXX_NUMREGS)
164 return -EINVAL;
165
Marc Reilly91b5e742012-04-01 16:41:37 +1000166 ret = regmap_read(mc13xxx->regmap, offset, val);
Marc Reilly5006fe52012-05-01 12:26:46 +0200167 dev_vdbg(mc13xxx->dev, "[0x%02x] -> 0x%06x\n", offset, *val);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200168
Marc Reilly5006fe52012-05-01 12:26:46 +0200169 return ret;
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200170}
171EXPORT_SYMBOL(mc13xxx_reg_read);
172
173int mc13xxx_reg_write(struct mc13xxx *mc13xxx, unsigned int offset, u32 val)
174{
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200175 BUG_ON(!mutex_is_locked(&mc13xxx->lock));
176
Marc Reilly5006fe52012-05-01 12:26:46 +0200177 dev_vdbg(mc13xxx->dev, "[0x%02x] <- 0x%06x\n", offset, val);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200178
179 if (offset > MC13XXX_NUMREGS || val > 0xffffff)
180 return -EINVAL;
181
Marc Reilly91b5e742012-04-01 16:41:37 +1000182 return regmap_write(mc13xxx->regmap, offset, val);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200183}
184EXPORT_SYMBOL(mc13xxx_reg_write);
185
186int mc13xxx_reg_rmw(struct mc13xxx *mc13xxx, unsigned int offset,
187 u32 mask, u32 val)
188{
Marc Reilly91b5e742012-04-01 16:41:37 +1000189 BUG_ON(!mutex_is_locked(&mc13xxx->lock));
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200190 BUG_ON(val & ~mask);
Marc Reilly91b5e742012-04-01 16:41:37 +1000191 dev_vdbg(mc13xxx->dev, "[0x%02x] <- 0x%06x (mask: 0x%06x)\n",
192 offset, val, mask);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200193
Marc Reilly91b5e742012-04-01 16:41:37 +1000194 return regmap_update_bits(mc13xxx->regmap, offset, mask, val);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200195}
196EXPORT_SYMBOL(mc13xxx_reg_rmw);
197
198int mc13xxx_irq_mask(struct mc13xxx *mc13xxx, int irq)
199{
200 int ret;
201 unsigned int offmask = irq < 24 ? MC13XXX_IRQMASK0 : MC13XXX_IRQMASK1;
202 u32 irqbit = 1 << (irq < 24 ? irq : irq - 24);
203 u32 mask;
204
205 if (irq < 0 || irq >= MC13XXX_NUM_IRQ)
206 return -EINVAL;
207
208 ret = mc13xxx_reg_read(mc13xxx, offmask, &mask);
209 if (ret)
210 return ret;
211
212 if (mask & irqbit)
213 /* already masked */
214 return 0;
215
216 return mc13xxx_reg_write(mc13xxx, offmask, mask | irqbit);
217}
218EXPORT_SYMBOL(mc13xxx_irq_mask);
219
220int mc13xxx_irq_unmask(struct mc13xxx *mc13xxx, int irq)
221{
222 int ret;
223 unsigned int offmask = irq < 24 ? MC13XXX_IRQMASK0 : MC13XXX_IRQMASK1;
224 u32 irqbit = 1 << (irq < 24 ? irq : irq - 24);
225 u32 mask;
226
227 if (irq < 0 || irq >= MC13XXX_NUM_IRQ)
228 return -EINVAL;
229
230 ret = mc13xxx_reg_read(mc13xxx, offmask, &mask);
231 if (ret)
232 return ret;
233
234 if (!(mask & irqbit))
235 /* already unmasked */
236 return 0;
237
238 return mc13xxx_reg_write(mc13xxx, offmask, mask & ~irqbit);
239}
240EXPORT_SYMBOL(mc13xxx_irq_unmask);
241
242int mc13xxx_irq_status(struct mc13xxx *mc13xxx, int irq,
243 int *enabled, int *pending)
244{
245 int ret;
246 unsigned int offmask = irq < 24 ? MC13XXX_IRQMASK0 : MC13XXX_IRQMASK1;
247 unsigned int offstat = irq < 24 ? MC13XXX_IRQSTAT0 : MC13XXX_IRQSTAT1;
248 u32 irqbit = 1 << (irq < 24 ? irq : irq - 24);
249
250 if (irq < 0 || irq >= MC13XXX_NUM_IRQ)
251 return -EINVAL;
252
253 if (enabled) {
254 u32 mask;
255
256 ret = mc13xxx_reg_read(mc13xxx, offmask, &mask);
257 if (ret)
258 return ret;
259
260 *enabled = mask & irqbit;
261 }
262
263 if (pending) {
264 u32 stat;
265
266 ret = mc13xxx_reg_read(mc13xxx, offstat, &stat);
267 if (ret)
268 return ret;
269
270 *pending = stat & irqbit;
271 }
272
273 return 0;
274}
275EXPORT_SYMBOL(mc13xxx_irq_status);
276
277int mc13xxx_irq_ack(struct mc13xxx *mc13xxx, int irq)
278{
279 unsigned int offstat = irq < 24 ? MC13XXX_IRQSTAT0 : MC13XXX_IRQSTAT1;
280 unsigned int val = 1 << (irq < 24 ? irq : irq - 24);
281
282 BUG_ON(irq < 0 || irq >= MC13XXX_NUM_IRQ);
283
284 return mc13xxx_reg_write(mc13xxx, offstat, val);
285}
286EXPORT_SYMBOL(mc13xxx_irq_ack);
287
288int mc13xxx_irq_request_nounmask(struct mc13xxx *mc13xxx, int irq,
289 irq_handler_t handler, const char *name, void *dev)
290{
291 BUG_ON(!mutex_is_locked(&mc13xxx->lock));
292 BUG_ON(!handler);
293
294 if (irq < 0 || irq >= MC13XXX_NUM_IRQ)
295 return -EINVAL;
296
297 if (mc13xxx->irqhandler[irq])
298 return -EBUSY;
299
300 mc13xxx->irqhandler[irq] = handler;
301 mc13xxx->irqdata[irq] = dev;
302
303 return 0;
304}
305EXPORT_SYMBOL(mc13xxx_irq_request_nounmask);
306
307int mc13xxx_irq_request(struct mc13xxx *mc13xxx, int irq,
308 irq_handler_t handler, const char *name, void *dev)
309{
310 int ret;
311
312 ret = mc13xxx_irq_request_nounmask(mc13xxx, irq, handler, name, dev);
313 if (ret)
314 return ret;
315
316 ret = mc13xxx_irq_unmask(mc13xxx, irq);
317 if (ret) {
318 mc13xxx->irqhandler[irq] = NULL;
319 mc13xxx->irqdata[irq] = NULL;
320 return ret;
321 }
322
323 return 0;
324}
325EXPORT_SYMBOL(mc13xxx_irq_request);
326
327int mc13xxx_irq_free(struct mc13xxx *mc13xxx, int irq, void *dev)
328{
329 int ret;
330 BUG_ON(!mutex_is_locked(&mc13xxx->lock));
331
332 if (irq < 0 || irq >= MC13XXX_NUM_IRQ || !mc13xxx->irqhandler[irq] ||
333 mc13xxx->irqdata[irq] != dev)
334 return -EINVAL;
335
336 ret = mc13xxx_irq_mask(mc13xxx, irq);
337 if (ret)
338 return ret;
339
340 mc13xxx->irqhandler[irq] = NULL;
341 mc13xxx->irqdata[irq] = NULL;
342
343 return 0;
344}
345EXPORT_SYMBOL(mc13xxx_irq_free);
346
347static inline irqreturn_t mc13xxx_irqhandler(struct mc13xxx *mc13xxx, int irq)
348{
349 return mc13xxx->irqhandler[irq](irq, mc13xxx->irqdata[irq]);
350}
351
352/*
353 * returns: number of handled irqs or negative error
354 * locking: holds mc13xxx->lock
355 */
356static int mc13xxx_irq_handle(struct mc13xxx *mc13xxx,
357 unsigned int offstat, unsigned int offmask, int baseirq)
358{
359 u32 stat, mask;
360 int ret = mc13xxx_reg_read(mc13xxx, offstat, &stat);
361 int num_handled = 0;
362
363 if (ret)
364 return ret;
365
366 ret = mc13xxx_reg_read(mc13xxx, offmask, &mask);
367 if (ret)
368 return ret;
369
370 while (stat & ~mask) {
371 int irq = __ffs(stat & ~mask);
372
373 stat &= ~(1 << irq);
374
375 if (likely(mc13xxx->irqhandler[baseirq + irq])) {
376 irqreturn_t handled;
377
378 handled = mc13xxx_irqhandler(mc13xxx, baseirq + irq);
379 if (handled == IRQ_HANDLED)
380 num_handled++;
381 } else {
Marc Reilly5006fe52012-05-01 12:26:46 +0200382 dev_err(mc13xxx->dev,
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200383 "BUG: irq %u but no handler\n",
384 baseirq + irq);
385
386 mask |= 1 << irq;
387
388 ret = mc13xxx_reg_write(mc13xxx, offmask, mask);
389 }
390 }
391
392 return num_handled;
393}
394
395static irqreturn_t mc13xxx_irq_thread(int irq, void *data)
396{
397 struct mc13xxx *mc13xxx = data;
398 irqreturn_t ret;
399 int handled = 0;
400
401 mc13xxx_lock(mc13xxx);
402
403 ret = mc13xxx_irq_handle(mc13xxx, MC13XXX_IRQSTAT0,
404 MC13XXX_IRQMASK0, 0);
405 if (ret > 0)
406 handled = 1;
407
408 ret = mc13xxx_irq_handle(mc13xxx, MC13XXX_IRQSTAT1,
409 MC13XXX_IRQMASK1, 24);
410 if (ret > 0)
411 handled = 1;
412
413 mc13xxx_unlock(mc13xxx);
414
415 return IRQ_RETVAL(handled);
416}
417
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200418#define maskval(reg, mask) (((reg) & (mask)) >> __ffs(mask))
Uwe Kleine-Königcd0f34b2012-07-12 09:57:52 +0000419static void mc13xxx_print_revision(struct mc13xxx *mc13xxx, u32 revision)
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200420{
Uwe Kleine-Königcd0f34b2012-07-12 09:57:52 +0000421 dev_info(mc13xxx->dev, "%s: rev: %d.%d, "
422 "fin: %d, fab: %d, icid: %d/%d\n",
423 mc13xxx->variant->name,
424 maskval(revision, MC13XXX_REVISION_REVFULL),
425 maskval(revision, MC13XXX_REVISION_REVMETAL),
426 maskval(revision, MC13XXX_REVISION_FIN),
427 maskval(revision, MC13XXX_REVISION_FAB),
428 maskval(revision, MC13XXX_REVISION_ICID),
429 maskval(revision, MC13XXX_REVISION_ICIDCODE));
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200430}
431
Uwe Kleine-König0312e022012-07-12 09:57:53 +0000432static void mc34708_print_revision(struct mc13xxx *mc13xxx, u32 revision)
433{
434 dev_info(mc13xxx->dev, "%s: rev %d.%d, fin: %d, fab: %d\n",
435 mc13xxx->variant->name,
436 maskval(revision, MC34708_REVISION_REVFULL),
437 maskval(revision, MC34708_REVISION_REVMETAL),
438 maskval(revision, MC34708_REVISION_FIN),
439 maskval(revision, MC34708_REVISION_FAB));
440}
441
Uwe Kleine-Königcd0f34b2012-07-12 09:57:52 +0000442/* These are only exported for mc13xxx-i2c and mc13xxx-spi */
443struct mc13xxx_variant mc13xxx_variant_mc13783 = {
444 .name = "mc13783",
445 .print_revision = mc13xxx_print_revision,
446};
447EXPORT_SYMBOL_GPL(mc13xxx_variant_mc13783);
448
449struct mc13xxx_variant mc13xxx_variant_mc13892 = {
450 .name = "mc13892",
451 .print_revision = mc13xxx_print_revision,
452};
453EXPORT_SYMBOL_GPL(mc13xxx_variant_mc13892);
454
Uwe Kleine-König0312e022012-07-12 09:57:53 +0000455struct mc13xxx_variant mc13xxx_variant_mc34708 = {
456 .name = "mc34708",
457 .print_revision = mc34708_print_revision,
458};
459EXPORT_SYMBOL_GPL(mc13xxx_variant_mc34708);
460
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200461static const char *mc13xxx_get_chipname(struct mc13xxx *mc13xxx)
462{
Uwe Kleine-Königcd0f34b2012-07-12 09:57:52 +0000463 return mc13xxx->variant->name;
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200464}
465
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200466int mc13xxx_get_flags(struct mc13xxx *mc13xxx)
467{
Shawn Guo876989d2011-12-12 18:52:57 +0100468 return mc13xxx->flags;
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200469}
470EXPORT_SYMBOL(mc13xxx_get_flags);
471
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200472#define MC13XXX_ADC1_CHAN0_SHIFT 5
473#define MC13XXX_ADC1_CHAN1_SHIFT 8
Michael Thalmeier1039d762012-02-20 12:18:13 +0100474#define MC13783_ADC1_ATO_SHIFT 11
475#define MC13783_ADC1_ATOX (1 << 19)
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200476
477struct mc13xxx_adcdone_data {
478 struct mc13xxx *mc13xxx;
479 struct completion done;
480};
481
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200482static irqreturn_t mc13xxx_handler_adcdone(int irq, void *data)
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200483{
484 struct mc13xxx_adcdone_data *adcdone_data = data;
485
486 mc13xxx_irq_ack(adcdone_data->mc13xxx, irq);
487
488 complete_all(&adcdone_data->done);
489
490 return IRQ_HANDLED;
491}
492
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200493#define MC13XXX_ADC_WORKING (1 << 0)
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200494
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200495int mc13xxx_adc_do_conversion(struct mc13xxx *mc13xxx, unsigned int mode,
Michael Thalmeier1039d762012-02-20 12:18:13 +0100496 unsigned int channel, u8 ato, bool atox,
497 unsigned int *sample)
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200498{
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200499 u32 adc0, adc1, old_adc0;
500 int i, ret;
501 struct mc13xxx_adcdone_data adcdone_data = {
502 .mc13xxx = mc13xxx,
503 };
504 init_completion(&adcdone_data.done);
505
Marc Reilly5006fe52012-05-01 12:26:46 +0200506 dev_dbg(mc13xxx->dev, "%s\n", __func__);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200507
508 mc13xxx_lock(mc13xxx);
509
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200510 if (mc13xxx->adcflags & MC13XXX_ADC_WORKING) {
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200511 ret = -EBUSY;
512 goto out;
513 }
514
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200515 mc13xxx->adcflags |= MC13XXX_ADC_WORKING;
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200516
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200517 mc13xxx_reg_read(mc13xxx, MC13XXX_ADC0, &old_adc0);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200518
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200519 adc0 = MC13XXX_ADC0_ADINC1 | MC13XXX_ADC0_ADINC2;
520 adc1 = MC13XXX_ADC1_ADEN | MC13XXX_ADC1_ADTRIGIGN | MC13XXX_ADC1_ASC;
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200521
522 if (channel > 7)
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200523 adc1 |= MC13XXX_ADC1_ADSEL;
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200524
525 switch (mode) {
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200526 case MC13XXX_ADC_MODE_TS:
527 adc0 |= MC13XXX_ADC0_ADREFEN | MC13XXX_ADC0_TSMOD0 |
528 MC13XXX_ADC0_TSMOD1;
529 adc1 |= 4 << MC13XXX_ADC1_CHAN1_SHIFT;
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200530 break;
531
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200532 case MC13XXX_ADC_MODE_SINGLE_CHAN:
Robin van der Gracht21618912011-11-29 12:09:03 +0100533 adc0 |= old_adc0 & MC13XXX_ADC0_CONFIG_MASK;
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200534 adc1 |= (channel & 0x7) << MC13XXX_ADC1_CHAN0_SHIFT;
535 adc1 |= MC13XXX_ADC1_RAND;
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200536 break;
537
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200538 case MC13XXX_ADC_MODE_MULT_CHAN:
Robin van der Gracht21618912011-11-29 12:09:03 +0100539 adc0 |= old_adc0 & MC13XXX_ADC0_CONFIG_MASK;
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200540 adc1 |= 4 << MC13XXX_ADC1_CHAN1_SHIFT;
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200541 break;
542
543 default:
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200544 mc13xxx_unlock(mc13xxx);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200545 return -EINVAL;
546 }
547
Michael Thalmeier1039d762012-02-20 12:18:13 +0100548 adc1 |= ato << MC13783_ADC1_ATO_SHIFT;
549 if (atox)
550 adc1 |= MC13783_ADC1_ATOX;
Marc Reilly5006fe52012-05-01 12:26:46 +0200551
552 dev_dbg(mc13xxx->dev, "%s: request irq\n", __func__);
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200553 mc13xxx_irq_request(mc13xxx, MC13XXX_IRQ_ADCDONE,
554 mc13xxx_handler_adcdone, __func__, &adcdone_data);
555 mc13xxx_irq_ack(mc13xxx, MC13XXX_IRQ_ADCDONE);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200556
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200557 mc13xxx_reg_write(mc13xxx, MC13XXX_ADC0, adc0);
558 mc13xxx_reg_write(mc13xxx, MC13XXX_ADC1, adc1);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200559
560 mc13xxx_unlock(mc13xxx);
561
562 ret = wait_for_completion_interruptible_timeout(&adcdone_data.done, HZ);
563
564 if (!ret)
565 ret = -ETIMEDOUT;
566
567 mc13xxx_lock(mc13xxx);
568
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200569 mc13xxx_irq_free(mc13xxx, MC13XXX_IRQ_ADCDONE, &adcdone_data);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200570
571 if (ret > 0)
572 for (i = 0; i < 4; ++i) {
573 ret = mc13xxx_reg_read(mc13xxx,
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200574 MC13XXX_ADC2, &sample[i]);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200575 if (ret)
576 break;
577 }
578
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200579 if (mode == MC13XXX_ADC_MODE_TS)
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200580 /* restore TSMOD */
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200581 mc13xxx_reg_write(mc13xxx, MC13XXX_ADC0, old_adc0);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200582
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200583 mc13xxx->adcflags &= ~MC13XXX_ADC_WORKING;
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200584out:
585 mc13xxx_unlock(mc13xxx);
586
587 return ret;
588}
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200589EXPORT_SYMBOL_GPL(mc13xxx_adc_do_conversion);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200590
591static int mc13xxx_add_subdevice_pdata(struct mc13xxx *mc13xxx,
Samuel Ortizc8a03c962011-04-08 01:55:01 +0200592 const char *format, void *pdata, size_t pdata_size)
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200593{
594 char buf[30];
595 const char *name = mc13xxx_get_chipname(mc13xxx);
596
597 struct mfd_cell cell = {
Samuel Ortizc8a03c962011-04-08 01:55:01 +0200598 .platform_data = pdata,
599 .pdata_size = pdata_size,
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200600 };
601
602 /* there is no asnprintf in the kernel :-( */
603 if (snprintf(buf, sizeof(buf), format, name) > sizeof(buf))
604 return -E2BIG;
605
606 cell.name = kmemdup(buf, strlen(buf) + 1, GFP_KERNEL);
607 if (!cell.name)
608 return -ENOMEM;
609
Mark Brown55692af2012-09-11 15:16:36 +0800610 return mfd_add_devices(mc13xxx->dev, -1, &cell, 1, NULL, 0, NULL);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200611}
612
613static int mc13xxx_add_subdevice(struct mc13xxx *mc13xxx, const char *format)
614{
Samuel Ortizc8a03c962011-04-08 01:55:01 +0200615 return mc13xxx_add_subdevice_pdata(mc13xxx, format, NULL, 0);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200616}
617
Shawn Guo876989d2011-12-12 18:52:57 +0100618#ifdef CONFIG_OF
619static int mc13xxx_probe_flags_dt(struct mc13xxx *mc13xxx)
620{
Marc Reilly91b5e742012-04-01 16:41:37 +1000621 struct device_node *np = mc13xxx->dev->of_node;
Shawn Guo876989d2011-12-12 18:52:57 +0100622
623 if (!np)
624 return -ENODEV;
625
626 if (of_get_property(np, "fsl,mc13xxx-uses-adc", NULL))
627 mc13xxx->flags |= MC13XXX_USE_ADC;
628
629 if (of_get_property(np, "fsl,mc13xxx-uses-codec", NULL))
630 mc13xxx->flags |= MC13XXX_USE_CODEC;
631
632 if (of_get_property(np, "fsl,mc13xxx-uses-rtc", NULL))
633 mc13xxx->flags |= MC13XXX_USE_RTC;
634
635 if (of_get_property(np, "fsl,mc13xxx-uses-touch", NULL))
636 mc13xxx->flags |= MC13XXX_USE_TOUCHSCREEN;
637
638 return 0;
639}
640#else
641static inline int mc13xxx_probe_flags_dt(struct mc13xxx *mc13xxx)
642{
643 return -ENODEV;
644}
645#endif
646
Marc Reillya0c7c1d2012-04-01 16:41:38 +1000647int mc13xxx_common_init(struct mc13xxx *mc13xxx,
Marc Reilly5006fe52012-05-01 12:26:46 +0200648 struct mc13xxx_platform_data *pdata, int irq)
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200649{
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200650 int ret;
Uwe Kleine-Königcd0f34b2012-07-12 09:57:52 +0000651 u32 revision;
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200652
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200653 mc13xxx_lock(mc13xxx);
654
Uwe Kleine-Königcd0f34b2012-07-12 09:57:52 +0000655 ret = mc13xxx_reg_read(mc13xxx, MC13XXX_REVISION, &revision);
Marc Reilly5006fe52012-05-01 12:26:46 +0200656 if (ret)
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200657 goto err_revision;
658
Uwe Kleine-Königcd0f34b2012-07-12 09:57:52 +0000659 mc13xxx->variant->print_revision(mc13xxx, revision);
660
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200661 /* mask all irqs */
662 ret = mc13xxx_reg_write(mc13xxx, MC13XXX_IRQMASK0, 0x00ffffff);
663 if (ret)
664 goto err_mask;
665
666 ret = mc13xxx_reg_write(mc13xxx, MC13XXX_IRQMASK1, 0x00ffffff);
667 if (ret)
668 goto err_mask;
669
Marc Reilly5006fe52012-05-01 12:26:46 +0200670 ret = request_threaded_irq(irq, NULL, mc13xxx_irq_thread,
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200671 IRQF_ONESHOT | IRQF_TRIGGER_HIGH, "mc13xxx", mc13xxx);
672
673 if (ret) {
674err_mask:
675err_revision:
Uwe Kleine-Könige1b88eb2010-11-11 16:47:50 +0100676 mc13xxx_unlock(mc13xxx);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200677 return ret;
678 }
679
Marc Reilly5006fe52012-05-01 12:26:46 +0200680 mc13xxx->irq = irq;
681
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200682 mc13xxx_unlock(mc13xxx);
683
Shawn Guo876989d2011-12-12 18:52:57 +0100684 if (mc13xxx_probe_flags_dt(mc13xxx) < 0 && pdata)
685 mc13xxx->flags = pdata->flags;
686
687 if (mc13xxx->flags & MC13XXX_USE_ADC)
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200688 mc13xxx_add_subdevice(mc13xxx, "%s-adc");
689
Shawn Guo876989d2011-12-12 18:52:57 +0100690 if (mc13xxx->flags & MC13XXX_USE_CODEC)
Philippe Rétornaze3a08712012-05-15 13:53:49 +0200691 mc13xxx_add_subdevice_pdata(mc13xxx, "%s-codec",
692 pdata->codec, sizeof(*pdata->codec));
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200693
Shawn Guo876989d2011-12-12 18:52:57 +0100694 if (mc13xxx->flags & MC13XXX_USE_RTC)
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200695 mc13xxx_add_subdevice(mc13xxx, "%s-rtc");
696
Shawn Guo876989d2011-12-12 18:52:57 +0100697 if (mc13xxx->flags & MC13XXX_USE_TOUCHSCREEN)
Michael Thalmeier1039d762012-02-20 12:18:13 +0100698 mc13xxx_add_subdevice_pdata(mc13xxx, "%s-ts",
699 &pdata->touch, sizeof(pdata->touch));
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200700
Shawn Guo876989d2011-12-12 18:52:57 +0100701 if (pdata) {
702 mc13xxx_add_subdevice_pdata(mc13xxx, "%s-regulator",
703 &pdata->regulators, sizeof(pdata->regulators));
Samuel Ortizc8a03c962011-04-08 01:55:01 +0200704 mc13xxx_add_subdevice_pdata(mc13xxx, "%s-led",
705 pdata->leds, sizeof(*pdata->leds));
Philippe Rétornaz30fc7ac2011-09-18 18:10:53 +0200706 mc13xxx_add_subdevice_pdata(mc13xxx, "%s-pwrbutton",
707 pdata->buttons, sizeof(*pdata->buttons));
Shawn Guo876989d2011-12-12 18:52:57 +0100708 } else {
709 mc13xxx_add_subdevice(mc13xxx, "%s-regulator");
710 mc13xxx_add_subdevice(mc13xxx, "%s-led");
711 mc13xxx_add_subdevice(mc13xxx, "%s-pwrbutton");
712 }
Philippe Rétornaz30fc7ac2011-09-18 18:10:53 +0200713
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200714 return 0;
715}
Marc Reillya0c7c1d2012-04-01 16:41:38 +1000716EXPORT_SYMBOL_GPL(mc13xxx_common_init);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200717
Marc Reillya0c7c1d2012-04-01 16:41:38 +1000718void mc13xxx_common_cleanup(struct mc13xxx *mc13xxx)
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200719{
Marc Reilly5006fe52012-05-01 12:26:46 +0200720 free_irq(mc13xxx->irq, mc13xxx);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200721
Marc Reilly5006fe52012-05-01 12:26:46 +0200722 mfd_remove_devices(mc13xxx->dev);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200723}
Marc Reillya0c7c1d2012-04-01 16:41:38 +1000724EXPORT_SYMBOL_GPL(mc13xxx_common_cleanup);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200725
726MODULE_DESCRIPTION("Core driver for Freescale MC13XXX PMIC");
727MODULE_AUTHOR("Uwe Kleine-Koenig <u.kleine-koenig@pengutronix.de>");
728MODULE_LICENSE("GPL v2");