blob: d0bf50e3568d7e770835092430e33175a4475601 [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
Uwe Kleine-König8e005932010-09-28 16:37:20 +020013#include <linux/module.h>
Shawn Guo876989d2011-12-12 18:52:57 +010014#include <linux/of.h>
15#include <linux/of_device.h>
Alexander Shiyan10f9eda2014-06-18 21:05:40 +040016#include <linux/platform_device.h>
17#include <linux/mfd/core.h>
Uwe Kleine-König8e005932010-09-28 16:37:20 +020018
Marc Reillya0c7c1d2012-04-01 16:41:38 +100019#include "mc13xxx.h"
Uwe Kleine-König8e005932010-09-28 16:37:20 +020020
Uwe Kleine-König8e005932010-09-28 16:37:20 +020021#define MC13XXX_IRQSTAT0 0
Uwe Kleine-König8e005932010-09-28 16:37:20 +020022#define MC13XXX_IRQMASK0 1
Uwe Kleine-König8e005932010-09-28 16:37:20 +020023#define MC13XXX_IRQSTAT1 3
Uwe Kleine-König8e005932010-09-28 16:37:20 +020024#define MC13XXX_IRQMASK1 4
Uwe Kleine-König8e005932010-09-28 16:37:20 +020025
26#define MC13XXX_REVISION 7
27#define MC13XXX_REVISION_REVMETAL (0x07 << 0)
28#define MC13XXX_REVISION_REVFULL (0x03 << 3)
29#define MC13XXX_REVISION_ICID (0x07 << 6)
30#define MC13XXX_REVISION_FIN (0x03 << 9)
31#define MC13XXX_REVISION_FAB (0x03 << 11)
32#define MC13XXX_REVISION_ICIDCODE (0x3f << 13)
33
Uwe Kleine-König0312e022012-07-12 09:57:53 +000034#define MC34708_REVISION_REVMETAL (0x07 << 0)
35#define MC34708_REVISION_REVFULL (0x07 << 3)
36#define MC34708_REVISION_FIN (0x07 << 6)
37#define MC34708_REVISION_FAB (0x07 << 9)
38
Markus Pargmann34a49582014-09-08 09:01:11 +020039#define MC13XXX_PWRCTRL 15
40#define MC13XXX_PWRCTRL_WDIRESET (1 << 12)
41
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +020042#define MC13XXX_ADC1 44
43#define MC13XXX_ADC1_ADEN (1 << 0)
44#define MC13XXX_ADC1_RAND (1 << 1)
45#define MC13XXX_ADC1_ADSEL (1 << 3)
46#define MC13XXX_ADC1_ASC (1 << 20)
47#define MC13XXX_ADC1_ADTRIGIGN (1 << 21)
Uwe Kleine-König8e005932010-09-28 16:37:20 +020048
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +020049#define MC13XXX_ADC2 45
Uwe Kleine-König8e005932010-09-28 16:37:20 +020050
Uwe Kleine-König8e005932010-09-28 16:37:20 +020051void mc13xxx_lock(struct mc13xxx *mc13xxx)
52{
53 if (!mutex_trylock(&mc13xxx->lock)) {
Scott Wood5d5a7bf2015-03-11 22:13:51 -050054 dev_dbg(mc13xxx->dev, "wait for %s from %ps\n",
Uwe Kleine-König8e005932010-09-28 16:37:20 +020055 __func__, __builtin_return_address(0));
56
57 mutex_lock(&mc13xxx->lock);
58 }
Scott Wood5d5a7bf2015-03-11 22:13:51 -050059 dev_dbg(mc13xxx->dev, "%s from %ps\n",
Uwe Kleine-König8e005932010-09-28 16:37:20 +020060 __func__, __builtin_return_address(0));
61}
62EXPORT_SYMBOL(mc13xxx_lock);
63
64void mc13xxx_unlock(struct mc13xxx *mc13xxx)
65{
Scott Wood5d5a7bf2015-03-11 22:13:51 -050066 dev_dbg(mc13xxx->dev, "%s from %ps\n",
Uwe Kleine-König8e005932010-09-28 16:37:20 +020067 __func__, __builtin_return_address(0));
68 mutex_unlock(&mc13xxx->lock);
69}
70EXPORT_SYMBOL(mc13xxx_unlock);
71
Uwe Kleine-König8e005932010-09-28 16:37:20 +020072int mc13xxx_reg_read(struct mc13xxx *mc13xxx, unsigned int offset, u32 *val)
73{
Uwe Kleine-König8e005932010-09-28 16:37:20 +020074 int ret;
75
Marc Reilly91b5e742012-04-01 16:41:37 +100076 ret = regmap_read(mc13xxx->regmap, offset, val);
Marc Reilly5006fe52012-05-01 12:26:46 +020077 dev_vdbg(mc13xxx->dev, "[0x%02x] -> 0x%06x\n", offset, *val);
Uwe Kleine-König8e005932010-09-28 16:37:20 +020078
Marc Reilly5006fe52012-05-01 12:26:46 +020079 return ret;
Uwe Kleine-König8e005932010-09-28 16:37:20 +020080}
81EXPORT_SYMBOL(mc13xxx_reg_read);
82
83int mc13xxx_reg_write(struct mc13xxx *mc13xxx, unsigned int offset, u32 val)
84{
Marc Reilly5006fe52012-05-01 12:26:46 +020085 dev_vdbg(mc13xxx->dev, "[0x%02x] <- 0x%06x\n", offset, val);
Uwe Kleine-König8e005932010-09-28 16:37:20 +020086
Alexander Shiyan328fe792013-12-07 13:00:43 +040087 if (val >= BIT(24))
Uwe Kleine-König8e005932010-09-28 16:37:20 +020088 return -EINVAL;
89
Marc Reilly91b5e742012-04-01 16:41:37 +100090 return regmap_write(mc13xxx->regmap, offset, val);
Uwe Kleine-König8e005932010-09-28 16:37:20 +020091}
92EXPORT_SYMBOL(mc13xxx_reg_write);
93
94int mc13xxx_reg_rmw(struct mc13xxx *mc13xxx, unsigned int offset,
95 u32 mask, u32 val)
96{
Uwe Kleine-König8e005932010-09-28 16:37:20 +020097 BUG_ON(val & ~mask);
Marc Reilly91b5e742012-04-01 16:41:37 +100098 dev_vdbg(mc13xxx->dev, "[0x%02x] <- 0x%06x (mask: 0x%06x)\n",
99 offset, val, mask);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200100
Marc Reilly91b5e742012-04-01 16:41:37 +1000101 return regmap_update_bits(mc13xxx->regmap, offset, mask, val);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200102}
103EXPORT_SYMBOL(mc13xxx_reg_rmw);
104
105int mc13xxx_irq_mask(struct mc13xxx *mc13xxx, int irq)
106{
Alexander Shiyan10f9eda2014-06-18 21:05:40 +0400107 int virq = regmap_irq_get_virq(mc13xxx->irq_data, irq);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200108
Alexander Shiyan10f9eda2014-06-18 21:05:40 +0400109 disable_irq_nosync(virq);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200110
Alexander Shiyan10f9eda2014-06-18 21:05:40 +0400111 return 0;
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200112}
113EXPORT_SYMBOL(mc13xxx_irq_mask);
114
115int mc13xxx_irq_unmask(struct mc13xxx *mc13xxx, int irq)
116{
Alexander Shiyan10f9eda2014-06-18 21:05:40 +0400117 int virq = regmap_irq_get_virq(mc13xxx->irq_data, irq);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200118
Alexander Shiyan10f9eda2014-06-18 21:05:40 +0400119 enable_irq(virq);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200120
Alexander Shiyan10f9eda2014-06-18 21:05:40 +0400121 return 0;
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200122}
123EXPORT_SYMBOL(mc13xxx_irq_unmask);
124
125int mc13xxx_irq_status(struct mc13xxx *mc13xxx, int irq,
126 int *enabled, int *pending)
127{
128 int ret;
129 unsigned int offmask = irq < 24 ? MC13XXX_IRQMASK0 : MC13XXX_IRQMASK1;
130 unsigned int offstat = irq < 24 ? MC13XXX_IRQSTAT0 : MC13XXX_IRQSTAT1;
131 u32 irqbit = 1 << (irq < 24 ? irq : irq - 24);
132
Alexander Shiyan10f9eda2014-06-18 21:05:40 +0400133 if (irq < 0 || irq >= ARRAY_SIZE(mc13xxx->irqs))
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200134 return -EINVAL;
135
136 if (enabled) {
137 u32 mask;
138
139 ret = mc13xxx_reg_read(mc13xxx, offmask, &mask);
140 if (ret)
141 return ret;
142
143 *enabled = mask & irqbit;
144 }
145
146 if (pending) {
147 u32 stat;
148
149 ret = mc13xxx_reg_read(mc13xxx, offstat, &stat);
150 if (ret)
151 return ret;
152
153 *pending = stat & irqbit;
154 }
155
156 return 0;
157}
158EXPORT_SYMBOL(mc13xxx_irq_status);
159
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200160int mc13xxx_irq_request(struct mc13xxx *mc13xxx, int irq,
161 irq_handler_t handler, const char *name, void *dev)
162{
Alexander Shiyan10f9eda2014-06-18 21:05:40 +0400163 int virq = regmap_irq_get_virq(mc13xxx->irq_data, irq);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200164
Alexander Shiyan10f9eda2014-06-18 21:05:40 +0400165 return devm_request_threaded_irq(mc13xxx->dev, virq, NULL, handler,
Fabio Estevam0b182c22015-05-16 15:42:11 -0300166 IRQF_ONESHOT, name, dev);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200167}
168EXPORT_SYMBOL(mc13xxx_irq_request);
169
170int mc13xxx_irq_free(struct mc13xxx *mc13xxx, int irq, void *dev)
171{
Alexander Shiyan10f9eda2014-06-18 21:05:40 +0400172 int virq = regmap_irq_get_virq(mc13xxx->irq_data, irq);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200173
Alexander Shiyan10f9eda2014-06-18 21:05:40 +0400174 devm_free_irq(mc13xxx->dev, virq, dev);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200175
176 return 0;
177}
178EXPORT_SYMBOL(mc13xxx_irq_free);
179
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200180#define maskval(reg, mask) (((reg) & (mask)) >> __ffs(mask))
Uwe Kleine-Königcd0f34b2012-07-12 09:57:52 +0000181static void mc13xxx_print_revision(struct mc13xxx *mc13xxx, u32 revision)
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200182{
Uwe Kleine-Königcd0f34b2012-07-12 09:57:52 +0000183 dev_info(mc13xxx->dev, "%s: rev: %d.%d, "
184 "fin: %d, fab: %d, icid: %d/%d\n",
185 mc13xxx->variant->name,
186 maskval(revision, MC13XXX_REVISION_REVFULL),
187 maskval(revision, MC13XXX_REVISION_REVMETAL),
188 maskval(revision, MC13XXX_REVISION_FIN),
189 maskval(revision, MC13XXX_REVISION_FAB),
190 maskval(revision, MC13XXX_REVISION_ICID),
191 maskval(revision, MC13XXX_REVISION_ICIDCODE));
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200192}
193
Uwe Kleine-König0312e022012-07-12 09:57:53 +0000194static void mc34708_print_revision(struct mc13xxx *mc13xxx, u32 revision)
195{
196 dev_info(mc13xxx->dev, "%s: rev %d.%d, fin: %d, fab: %d\n",
197 mc13xxx->variant->name,
198 maskval(revision, MC34708_REVISION_REVFULL),
199 maskval(revision, MC34708_REVISION_REVMETAL),
200 maskval(revision, MC34708_REVISION_FIN),
201 maskval(revision, MC34708_REVISION_FAB));
202}
203
Uwe Kleine-Königcd0f34b2012-07-12 09:57:52 +0000204/* These are only exported for mc13xxx-i2c and mc13xxx-spi */
205struct mc13xxx_variant mc13xxx_variant_mc13783 = {
206 .name = "mc13783",
207 .print_revision = mc13xxx_print_revision,
208};
209EXPORT_SYMBOL_GPL(mc13xxx_variant_mc13783);
210
211struct mc13xxx_variant mc13xxx_variant_mc13892 = {
212 .name = "mc13892",
213 .print_revision = mc13xxx_print_revision,
214};
215EXPORT_SYMBOL_GPL(mc13xxx_variant_mc13892);
216
Uwe Kleine-König0312e022012-07-12 09:57:53 +0000217struct mc13xxx_variant mc13xxx_variant_mc34708 = {
218 .name = "mc34708",
219 .print_revision = mc34708_print_revision,
220};
221EXPORT_SYMBOL_GPL(mc13xxx_variant_mc34708);
222
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200223static const char *mc13xxx_get_chipname(struct mc13xxx *mc13xxx)
224{
Uwe Kleine-Königcd0f34b2012-07-12 09:57:52 +0000225 return mc13xxx->variant->name;
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200226}
227
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200228int mc13xxx_get_flags(struct mc13xxx *mc13xxx)
229{
Shawn Guo876989d2011-12-12 18:52:57 +0100230 return mc13xxx->flags;
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200231}
232EXPORT_SYMBOL(mc13xxx_get_flags);
233
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200234#define MC13XXX_ADC1_CHAN0_SHIFT 5
235#define MC13XXX_ADC1_CHAN1_SHIFT 8
Michael Thalmeier1039d762012-02-20 12:18:13 +0100236#define MC13783_ADC1_ATO_SHIFT 11
237#define MC13783_ADC1_ATOX (1 << 19)
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200238
239struct mc13xxx_adcdone_data {
240 struct mc13xxx *mc13xxx;
241 struct completion done;
242};
243
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200244static irqreturn_t mc13xxx_handler_adcdone(int irq, void *data)
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200245{
246 struct mc13xxx_adcdone_data *adcdone_data = data;
247
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200248 complete_all(&adcdone_data->done);
249
250 return IRQ_HANDLED;
251}
252
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200253#define MC13XXX_ADC_WORKING (1 << 0)
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200254
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200255int mc13xxx_adc_do_conversion(struct mc13xxx *mc13xxx, unsigned int mode,
Michael Thalmeier1039d762012-02-20 12:18:13 +0100256 unsigned int channel, u8 ato, bool atox,
257 unsigned int *sample)
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200258{
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200259 u32 adc0, adc1, old_adc0;
260 int i, ret;
261 struct mc13xxx_adcdone_data adcdone_data = {
262 .mc13xxx = mc13xxx,
263 };
264 init_completion(&adcdone_data.done);
265
Marc Reilly5006fe52012-05-01 12:26:46 +0200266 dev_dbg(mc13xxx->dev, "%s\n", __func__);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200267
268 mc13xxx_lock(mc13xxx);
269
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200270 if (mc13xxx->adcflags & MC13XXX_ADC_WORKING) {
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200271 ret = -EBUSY;
272 goto out;
273 }
274
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200275 mc13xxx->adcflags |= MC13XXX_ADC_WORKING;
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200276
Kangjie Lu9e289892018-12-20 15:12:11 -0600277 ret = mc13xxx_reg_read(mc13xxx, MC13XXX_ADC0, &old_adc0);
278 if (ret)
279 goto out;
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200280
Fabio Estevam551434392018-08-28 17:02:40 -0300281 adc0 = MC13XXX_ADC0_ADINC1 | MC13XXX_ADC0_ADINC2 |
282 MC13XXX_ADC0_CHRGRAWDIV;
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200283 adc1 = MC13XXX_ADC1_ADEN | MC13XXX_ADC1_ADTRIGIGN | MC13XXX_ADC1_ASC;
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200284
Andrey Gusakoved645cc2018-03-27 17:19:49 +0300285 /*
286 * Channels mapped through ADIN7:
287 * 7 - General purpose ADIN7
288 * 16 - UID
289 * 17 - Die temperature
290 */
291 if (channel > 7 && channel < 16) {
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200292 adc1 |= MC13XXX_ADC1_ADSEL;
Andrey Gusakoved645cc2018-03-27 17:19:49 +0300293 } else if (channel == 16) {
294 adc0 |= MC13XXX_ADC0_ADIN7SEL_UID;
295 channel = 7;
296 } else if (channel == 17) {
297 adc0 |= MC13XXX_ADC0_ADIN7SEL_DIE;
298 channel = 7;
299 }
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200300
301 switch (mode) {
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200302 case MC13XXX_ADC_MODE_TS:
303 adc0 |= MC13XXX_ADC0_ADREFEN | MC13XXX_ADC0_TSMOD0 |
304 MC13XXX_ADC0_TSMOD1;
305 adc1 |= 4 << MC13XXX_ADC1_CHAN1_SHIFT;
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200306 break;
307
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200308 case MC13XXX_ADC_MODE_SINGLE_CHAN:
Robin van der Gracht21618912011-11-29 12:09:03 +0100309 adc0 |= old_adc0 & MC13XXX_ADC0_CONFIG_MASK;
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200310 adc1 |= (channel & 0x7) << MC13XXX_ADC1_CHAN0_SHIFT;
311 adc1 |= MC13XXX_ADC1_RAND;
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200312 break;
313
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200314 case MC13XXX_ADC_MODE_MULT_CHAN:
Robin van der Gracht21618912011-11-29 12:09:03 +0100315 adc0 |= old_adc0 & MC13XXX_ADC0_CONFIG_MASK;
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200316 adc1 |= 4 << MC13XXX_ADC1_CHAN1_SHIFT;
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200317 break;
318
319 default:
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200320 mc13xxx_unlock(mc13xxx);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200321 return -EINVAL;
322 }
323
Michael Thalmeier1039d762012-02-20 12:18:13 +0100324 adc1 |= ato << MC13783_ADC1_ATO_SHIFT;
325 if (atox)
326 adc1 |= MC13783_ADC1_ATOX;
Marc Reilly5006fe52012-05-01 12:26:46 +0200327
328 dev_dbg(mc13xxx->dev, "%s: request irq\n", __func__);
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200329 mc13xxx_irq_request(mc13xxx, MC13XXX_IRQ_ADCDONE,
330 mc13xxx_handler_adcdone, __func__, &adcdone_data);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200331
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200332 mc13xxx_reg_write(mc13xxx, MC13XXX_ADC0, adc0);
333 mc13xxx_reg_write(mc13xxx, MC13XXX_ADC1, adc1);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200334
335 mc13xxx_unlock(mc13xxx);
336
337 ret = wait_for_completion_interruptible_timeout(&adcdone_data.done, HZ);
338
339 if (!ret)
340 ret = -ETIMEDOUT;
341
342 mc13xxx_lock(mc13xxx);
343
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200344 mc13xxx_irq_free(mc13xxx, MC13XXX_IRQ_ADCDONE, &adcdone_data);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200345
346 if (ret > 0)
347 for (i = 0; i < 4; ++i) {
348 ret = mc13xxx_reg_read(mc13xxx,
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200349 MC13XXX_ADC2, &sample[i]);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200350 if (ret)
351 break;
352 }
353
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200354 if (mode == MC13XXX_ADC_MODE_TS)
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200355 /* restore TSMOD */
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200356 mc13xxx_reg_write(mc13xxx, MC13XXX_ADC0, old_adc0);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200357
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200358 mc13xxx->adcflags &= ~MC13XXX_ADC_WORKING;
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200359out:
360 mc13xxx_unlock(mc13xxx);
361
362 return ret;
363}
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200364EXPORT_SYMBOL_GPL(mc13xxx_adc_do_conversion);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200365
366static int mc13xxx_add_subdevice_pdata(struct mc13xxx *mc13xxx,
Samuel Ortizc8a03c92011-04-08 01:55:01 +0200367 const char *format, void *pdata, size_t pdata_size)
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200368{
369 char buf[30];
370 const char *name = mc13xxx_get_chipname(mc13xxx);
371
372 struct mfd_cell cell = {
Samuel Ortizc8a03c92011-04-08 01:55:01 +0200373 .platform_data = pdata,
374 .pdata_size = pdata_size,
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200375 };
376
377 /* there is no asnprintf in the kernel :-( */
378 if (snprintf(buf, sizeof(buf), format, name) > sizeof(buf))
379 return -E2BIG;
380
381 cell.name = kmemdup(buf, strlen(buf) + 1, GFP_KERNEL);
382 if (!cell.name)
383 return -ENOMEM;
384
Alexander Shiyan10f9eda2014-06-18 21:05:40 +0400385 return mfd_add_devices(mc13xxx->dev, -1, &cell, 1, NULL, 0,
386 regmap_irq_get_domain(mc13xxx->irq_data));
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200387}
388
389static int mc13xxx_add_subdevice(struct mc13xxx *mc13xxx, const char *format)
390{
Samuel Ortizc8a03c92011-04-08 01:55:01 +0200391 return mc13xxx_add_subdevice_pdata(mc13xxx, format, NULL, 0);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200392}
393
Shawn Guo876989d2011-12-12 18:52:57 +0100394#ifdef CONFIG_OF
395static int mc13xxx_probe_flags_dt(struct mc13xxx *mc13xxx)
396{
Marc Reilly91b5e742012-04-01 16:41:37 +1000397 struct device_node *np = mc13xxx->dev->of_node;
Shawn Guo876989d2011-12-12 18:52:57 +0100398
399 if (!np)
400 return -ENODEV;
401
Saurabh Sengar505b9e52015-11-16 14:43:17 +0530402 if (of_property_read_bool(np, "fsl,mc13xxx-uses-adc"))
Shawn Guo876989d2011-12-12 18:52:57 +0100403 mc13xxx->flags |= MC13XXX_USE_ADC;
404
Saurabh Sengar505b9e52015-11-16 14:43:17 +0530405 if (of_property_read_bool(np, "fsl,mc13xxx-uses-codec"))
Shawn Guo876989d2011-12-12 18:52:57 +0100406 mc13xxx->flags |= MC13XXX_USE_CODEC;
407
Saurabh Sengar505b9e52015-11-16 14:43:17 +0530408 if (of_property_read_bool(np, "fsl,mc13xxx-uses-rtc"))
Shawn Guo876989d2011-12-12 18:52:57 +0100409 mc13xxx->flags |= MC13XXX_USE_RTC;
410
Saurabh Sengar505b9e52015-11-16 14:43:17 +0530411 if (of_property_read_bool(np, "fsl,mc13xxx-uses-touch"))
Shawn Guo876989d2011-12-12 18:52:57 +0100412 mc13xxx->flags |= MC13XXX_USE_TOUCHSCREEN;
413
414 return 0;
415}
416#else
417static inline int mc13xxx_probe_flags_dt(struct mc13xxx *mc13xxx)
418{
419 return -ENODEV;
420}
421#endif
422
Alexander Shiyandb9ef442013-12-14 17:03:12 +0400423int mc13xxx_common_init(struct device *dev)
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200424{
Alexander Shiyandb9ef442013-12-14 17:03:12 +0400425 struct mc13xxx_platform_data *pdata = dev_get_platdata(dev);
426 struct mc13xxx *mc13xxx = dev_get_drvdata(dev);
Uwe Kleine-Königcd0f34b2012-07-12 09:57:52 +0000427 u32 revision;
Alexander Shiyan10f9eda2014-06-18 21:05:40 +0400428 int i, ret;
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200429
Alexander Shiyandb9ef442013-12-14 17:03:12 +0400430 mc13xxx->dev = dev;
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200431
Uwe Kleine-Königcd0f34b2012-07-12 09:57:52 +0000432 ret = mc13xxx_reg_read(mc13xxx, MC13XXX_REVISION, &revision);
Marc Reilly5006fe52012-05-01 12:26:46 +0200433 if (ret)
Alexander Shiyandb9ef442013-12-14 17:03:12 +0400434 return ret;
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200435
Uwe Kleine-Königcd0f34b2012-07-12 09:57:52 +0000436 mc13xxx->variant->print_revision(mc13xxx, revision);
437
Markus Pargmann34a49582014-09-08 09:01:11 +0200438 ret = mc13xxx_reg_rmw(mc13xxx, MC13XXX_PWRCTRL,
439 MC13XXX_PWRCTRL_WDIRESET, MC13XXX_PWRCTRL_WDIRESET);
440 if (ret)
441 return ret;
442
Alexander Shiyan10f9eda2014-06-18 21:05:40 +0400443 for (i = 0; i < ARRAY_SIZE(mc13xxx->irqs); i++) {
444 mc13xxx->irqs[i].reg_offset = i / MC13XXX_IRQ_PER_REG;
445 mc13xxx->irqs[i].mask = BIT(i % MC13XXX_IRQ_PER_REG);
446 }
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200447
Alexander Shiyan10f9eda2014-06-18 21:05:40 +0400448 mc13xxx->irq_chip.name = dev_name(dev);
449 mc13xxx->irq_chip.status_base = MC13XXX_IRQSTAT0;
450 mc13xxx->irq_chip.mask_base = MC13XXX_IRQMASK0;
451 mc13xxx->irq_chip.ack_base = MC13XXX_IRQSTAT0;
452 mc13xxx->irq_chip.irq_reg_stride = MC13XXX_IRQSTAT1 - MC13XXX_IRQSTAT0;
453 mc13xxx->irq_chip.init_ack_masked = true;
454 mc13xxx->irq_chip.use_ack = true;
455 mc13xxx->irq_chip.num_regs = MC13XXX_IRQ_REG_CNT;
456 mc13xxx->irq_chip.irqs = mc13xxx->irqs;
457 mc13xxx->irq_chip.num_irqs = ARRAY_SIZE(mc13xxx->irqs);
458
459 ret = regmap_add_irq_chip(mc13xxx->regmap, mc13xxx->irq, IRQF_ONESHOT,
460 0, &mc13xxx->irq_chip, &mc13xxx->irq_data);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200461 if (ret)
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200462 return ret;
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200463
Fabio Estevam48ca9a52014-04-15 19:56:14 -0300464 mutex_init(&mc13xxx->lock);
465
Shawn Guo876989d2011-12-12 18:52:57 +0100466 if (mc13xxx_probe_flags_dt(mc13xxx) < 0 && pdata)
467 mc13xxx->flags = pdata->flags;
468
Shawn Guo876989d2011-12-12 18:52:57 +0100469 if (pdata) {
470 mc13xxx_add_subdevice_pdata(mc13xxx, "%s-regulator",
471 &pdata->regulators, sizeof(pdata->regulators));
Samuel Ortizc8a03c92011-04-08 01:55:01 +0200472 mc13xxx_add_subdevice_pdata(mc13xxx, "%s-led",
473 pdata->leds, sizeof(*pdata->leds));
Philippe Rétornaz30fc7ac2011-09-18 18:10:53 +0200474 mc13xxx_add_subdevice_pdata(mc13xxx, "%s-pwrbutton",
475 pdata->buttons, sizeof(*pdata->buttons));
Alexander Shiyana2ff8452014-05-01 10:48:03 +0400476 if (mc13xxx->flags & MC13XXX_USE_CODEC)
477 mc13xxx_add_subdevice_pdata(mc13xxx, "%s-codec",
478 pdata->codec, sizeof(*pdata->codec));
479 if (mc13xxx->flags & MC13XXX_USE_TOUCHSCREEN)
480 mc13xxx_add_subdevice_pdata(mc13xxx, "%s-ts",
481 &pdata->touch, sizeof(pdata->touch));
Shawn Guo876989d2011-12-12 18:52:57 +0100482 } else {
483 mc13xxx_add_subdevice(mc13xxx, "%s-regulator");
484 mc13xxx_add_subdevice(mc13xxx, "%s-led");
485 mc13xxx_add_subdevice(mc13xxx, "%s-pwrbutton");
Alexander Shiyana2ff8452014-05-01 10:48:03 +0400486 if (mc13xxx->flags & MC13XXX_USE_CODEC)
487 mc13xxx_add_subdevice(mc13xxx, "%s-codec");
488 if (mc13xxx->flags & MC13XXX_USE_TOUCHSCREEN)
489 mc13xxx_add_subdevice(mc13xxx, "%s-ts");
Shawn Guo876989d2011-12-12 18:52:57 +0100490 }
Philippe Rétornaz30fc7ac2011-09-18 18:10:53 +0200491
Alexander Shiyan10f9eda2014-06-18 21:05:40 +0400492 if (mc13xxx->flags & MC13XXX_USE_ADC)
493 mc13xxx_add_subdevice(mc13xxx, "%s-adc");
494
495 if (mc13xxx->flags & MC13XXX_USE_RTC)
496 mc13xxx_add_subdevice(mc13xxx, "%s-rtc");
497
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200498 return 0;
499}
Marc Reillya0c7c1d2012-04-01 16:41:38 +1000500EXPORT_SYMBOL_GPL(mc13xxx_common_init);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200501
Alexander Shiyandb9ef442013-12-14 17:03:12 +0400502int mc13xxx_common_exit(struct device *dev)
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200503{
Alexander Shiyandb9ef442013-12-14 17:03:12 +0400504 struct mc13xxx *mc13xxx = dev_get_drvdata(dev);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200505
Alexander Shiyandb9ef442013-12-14 17:03:12 +0400506 mfd_remove_devices(dev);
Alexander Shiyan10f9eda2014-06-18 21:05:40 +0400507 regmap_del_irq_chip(mc13xxx->irq, mc13xxx->irq_data);
Alexander Shiyandb9ef442013-12-14 17:03:12 +0400508 mutex_destroy(&mc13xxx->lock);
509
510 return 0;
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200511}
Alexander Shiyandb9ef442013-12-14 17:03:12 +0400512EXPORT_SYMBOL_GPL(mc13xxx_common_exit);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200513
514MODULE_DESCRIPTION("Core driver for Freescale MC13XXX PMIC");
515MODULE_AUTHOR("Uwe Kleine-Koenig <u.kleine-koenig@pengutronix.de>");
516MODULE_LICENSE("GPL v2");