blob: 1abe7432aad82a308d6b3d8f4c977e9a91964edd [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Uwe Kleine-König8e005932010-09-28 16:37:20 +02002/*
3 * Copyright 2009-2010 Pengutronix
4 * Uwe Kleine-Koenig <u.kleine-koenig@pengutronix.de>
5 *
6 * loosely based on an earlier driver that has
7 * Copyright 2009 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
Uwe Kleine-König8e005932010-09-28 16:37:20 +02008 */
Uwe Kleine-König8e005932010-09-28 16:37:20 +02009
Uwe Kleine-König8e005932010-09-28 16:37:20 +020010#include <linux/module.h>
Shawn Guo876989d2011-12-12 18:52:57 +010011#include <linux/of.h>
12#include <linux/of_device.h>
Alexander Shiyan10f9eda2014-06-18 21:05:40 +040013#include <linux/platform_device.h>
14#include <linux/mfd/core.h>
Uwe Kleine-König8e005932010-09-28 16:37:20 +020015
Marc Reillya0c7c1d2012-04-01 16:41:38 +100016#include "mc13xxx.h"
Uwe Kleine-König8e005932010-09-28 16:37:20 +020017
Uwe Kleine-König8e005932010-09-28 16:37:20 +020018#define MC13XXX_IRQSTAT0 0
Uwe Kleine-König8e005932010-09-28 16:37:20 +020019#define MC13XXX_IRQMASK0 1
Uwe Kleine-König8e005932010-09-28 16:37:20 +020020#define MC13XXX_IRQSTAT1 3
Uwe Kleine-König8e005932010-09-28 16:37:20 +020021#define MC13XXX_IRQMASK1 4
Uwe Kleine-König8e005932010-09-28 16:37:20 +020022
23#define MC13XXX_REVISION 7
24#define MC13XXX_REVISION_REVMETAL (0x07 << 0)
25#define MC13XXX_REVISION_REVFULL (0x03 << 3)
26#define MC13XXX_REVISION_ICID (0x07 << 6)
27#define MC13XXX_REVISION_FIN (0x03 << 9)
28#define MC13XXX_REVISION_FAB (0x03 << 11)
29#define MC13XXX_REVISION_ICIDCODE (0x3f << 13)
30
Uwe Kleine-König0312e022012-07-12 09:57:53 +000031#define MC34708_REVISION_REVMETAL (0x07 << 0)
32#define MC34708_REVISION_REVFULL (0x07 << 3)
33#define MC34708_REVISION_FIN (0x07 << 6)
34#define MC34708_REVISION_FAB (0x07 << 9)
35
Markus Pargmann34a49582014-09-08 09:01:11 +020036#define MC13XXX_PWRCTRL 15
37#define MC13XXX_PWRCTRL_WDIRESET (1 << 12)
38
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +020039#define MC13XXX_ADC1 44
40#define MC13XXX_ADC1_ADEN (1 << 0)
41#define MC13XXX_ADC1_RAND (1 << 1)
42#define MC13XXX_ADC1_ADSEL (1 << 3)
43#define MC13XXX_ADC1_ASC (1 << 20)
44#define MC13XXX_ADC1_ADTRIGIGN (1 << 21)
Uwe Kleine-König8e005932010-09-28 16:37:20 +020045
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +020046#define MC13XXX_ADC2 45
Uwe Kleine-König8e005932010-09-28 16:37:20 +020047
Uwe Kleine-König8e005932010-09-28 16:37:20 +020048void mc13xxx_lock(struct mc13xxx *mc13xxx)
49{
50 if (!mutex_trylock(&mc13xxx->lock)) {
Scott Wood5d5a7bf2015-03-11 22:13:51 -050051 dev_dbg(mc13xxx->dev, "wait for %s from %ps\n",
Uwe Kleine-König8e005932010-09-28 16:37:20 +020052 __func__, __builtin_return_address(0));
53
54 mutex_lock(&mc13xxx->lock);
55 }
Scott Wood5d5a7bf2015-03-11 22:13:51 -050056 dev_dbg(mc13xxx->dev, "%s from %ps\n",
Uwe Kleine-König8e005932010-09-28 16:37:20 +020057 __func__, __builtin_return_address(0));
58}
59EXPORT_SYMBOL(mc13xxx_lock);
60
61void mc13xxx_unlock(struct mc13xxx *mc13xxx)
62{
Scott Wood5d5a7bf2015-03-11 22:13:51 -050063 dev_dbg(mc13xxx->dev, "%s from %ps\n",
Uwe Kleine-König8e005932010-09-28 16:37:20 +020064 __func__, __builtin_return_address(0));
65 mutex_unlock(&mc13xxx->lock);
66}
67EXPORT_SYMBOL(mc13xxx_unlock);
68
Uwe Kleine-König8e005932010-09-28 16:37:20 +020069int mc13xxx_reg_read(struct mc13xxx *mc13xxx, unsigned int offset, u32 *val)
70{
Uwe Kleine-König8e005932010-09-28 16:37:20 +020071 int ret;
72
Marc Reilly91b5e742012-04-01 16:41:37 +100073 ret = regmap_read(mc13xxx->regmap, offset, val);
Marc Reilly5006fe52012-05-01 12:26:46 +020074 dev_vdbg(mc13xxx->dev, "[0x%02x] -> 0x%06x\n", offset, *val);
Uwe Kleine-König8e005932010-09-28 16:37:20 +020075
Marc Reilly5006fe52012-05-01 12:26:46 +020076 return ret;
Uwe Kleine-König8e005932010-09-28 16:37:20 +020077}
78EXPORT_SYMBOL(mc13xxx_reg_read);
79
80int mc13xxx_reg_write(struct mc13xxx *mc13xxx, unsigned int offset, u32 val)
81{
Marc Reilly5006fe52012-05-01 12:26:46 +020082 dev_vdbg(mc13xxx->dev, "[0x%02x] <- 0x%06x\n", offset, val);
Uwe Kleine-König8e005932010-09-28 16:37:20 +020083
Alexander Shiyan328fe792013-12-07 13:00:43 +040084 if (val >= BIT(24))
Uwe Kleine-König8e005932010-09-28 16:37:20 +020085 return -EINVAL;
86
Marc Reilly91b5e742012-04-01 16:41:37 +100087 return regmap_write(mc13xxx->regmap, offset, val);
Uwe Kleine-König8e005932010-09-28 16:37:20 +020088}
89EXPORT_SYMBOL(mc13xxx_reg_write);
90
91int mc13xxx_reg_rmw(struct mc13xxx *mc13xxx, unsigned int offset,
92 u32 mask, u32 val)
93{
Uwe Kleine-König8e005932010-09-28 16:37:20 +020094 BUG_ON(val & ~mask);
Marc Reilly91b5e742012-04-01 16:41:37 +100095 dev_vdbg(mc13xxx->dev, "[0x%02x] <- 0x%06x (mask: 0x%06x)\n",
96 offset, val, mask);
Uwe Kleine-König8e005932010-09-28 16:37:20 +020097
Marc Reilly91b5e742012-04-01 16:41:37 +100098 return regmap_update_bits(mc13xxx->regmap, offset, mask, val);
Uwe Kleine-König8e005932010-09-28 16:37:20 +020099}
100EXPORT_SYMBOL(mc13xxx_reg_rmw);
101
102int mc13xxx_irq_mask(struct mc13xxx *mc13xxx, int irq)
103{
Alexander Shiyan10f9eda2014-06-18 21:05:40 +0400104 int virq = regmap_irq_get_virq(mc13xxx->irq_data, irq);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200105
Alexander Shiyan10f9eda2014-06-18 21:05:40 +0400106 disable_irq_nosync(virq);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200107
Alexander Shiyan10f9eda2014-06-18 21:05:40 +0400108 return 0;
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200109}
110EXPORT_SYMBOL(mc13xxx_irq_mask);
111
112int mc13xxx_irq_unmask(struct mc13xxx *mc13xxx, int irq)
113{
Alexander Shiyan10f9eda2014-06-18 21:05:40 +0400114 int virq = regmap_irq_get_virq(mc13xxx->irq_data, irq);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200115
Alexander Shiyan10f9eda2014-06-18 21:05:40 +0400116 enable_irq(virq);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200117
Alexander Shiyan10f9eda2014-06-18 21:05:40 +0400118 return 0;
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200119}
120EXPORT_SYMBOL(mc13xxx_irq_unmask);
121
122int mc13xxx_irq_status(struct mc13xxx *mc13xxx, int irq,
123 int *enabled, int *pending)
124{
125 int ret;
126 unsigned int offmask = irq < 24 ? MC13XXX_IRQMASK0 : MC13XXX_IRQMASK1;
127 unsigned int offstat = irq < 24 ? MC13XXX_IRQSTAT0 : MC13XXX_IRQSTAT1;
128 u32 irqbit = 1 << (irq < 24 ? irq : irq - 24);
129
Alexander Shiyan10f9eda2014-06-18 21:05:40 +0400130 if (irq < 0 || irq >= ARRAY_SIZE(mc13xxx->irqs))
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200131 return -EINVAL;
132
133 if (enabled) {
134 u32 mask;
135
136 ret = mc13xxx_reg_read(mc13xxx, offmask, &mask);
137 if (ret)
138 return ret;
139
140 *enabled = mask & irqbit;
141 }
142
143 if (pending) {
144 u32 stat;
145
146 ret = mc13xxx_reg_read(mc13xxx, offstat, &stat);
147 if (ret)
148 return ret;
149
150 *pending = stat & irqbit;
151 }
152
153 return 0;
154}
155EXPORT_SYMBOL(mc13xxx_irq_status);
156
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200157int mc13xxx_irq_request(struct mc13xxx *mc13xxx, int irq,
158 irq_handler_t handler, const char *name, void *dev)
159{
Alexander Shiyan10f9eda2014-06-18 21:05:40 +0400160 int virq = regmap_irq_get_virq(mc13xxx->irq_data, irq);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200161
Alexander Shiyan10f9eda2014-06-18 21:05:40 +0400162 return devm_request_threaded_irq(mc13xxx->dev, virq, NULL, handler,
Fabio Estevam0b182c22015-05-16 15:42:11 -0300163 IRQF_ONESHOT, name, dev);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200164}
165EXPORT_SYMBOL(mc13xxx_irq_request);
166
167int mc13xxx_irq_free(struct mc13xxx *mc13xxx, int irq, void *dev)
168{
Alexander Shiyan10f9eda2014-06-18 21:05:40 +0400169 int virq = regmap_irq_get_virq(mc13xxx->irq_data, irq);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200170
Alexander Shiyan10f9eda2014-06-18 21:05:40 +0400171 devm_free_irq(mc13xxx->dev, virq, dev);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200172
173 return 0;
174}
175EXPORT_SYMBOL(mc13xxx_irq_free);
176
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200177#define maskval(reg, mask) (((reg) & (mask)) >> __ffs(mask))
Uwe Kleine-Königcd0f34b2012-07-12 09:57:52 +0000178static void mc13xxx_print_revision(struct mc13xxx *mc13xxx, u32 revision)
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200179{
Uwe Kleine-Königcd0f34b2012-07-12 09:57:52 +0000180 dev_info(mc13xxx->dev, "%s: rev: %d.%d, "
181 "fin: %d, fab: %d, icid: %d/%d\n",
182 mc13xxx->variant->name,
183 maskval(revision, MC13XXX_REVISION_REVFULL),
184 maskval(revision, MC13XXX_REVISION_REVMETAL),
185 maskval(revision, MC13XXX_REVISION_FIN),
186 maskval(revision, MC13XXX_REVISION_FAB),
187 maskval(revision, MC13XXX_REVISION_ICID),
188 maskval(revision, MC13XXX_REVISION_ICIDCODE));
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200189}
190
Uwe Kleine-König0312e022012-07-12 09:57:53 +0000191static void mc34708_print_revision(struct mc13xxx *mc13xxx, u32 revision)
192{
193 dev_info(mc13xxx->dev, "%s: rev %d.%d, fin: %d, fab: %d\n",
194 mc13xxx->variant->name,
195 maskval(revision, MC34708_REVISION_REVFULL),
196 maskval(revision, MC34708_REVISION_REVMETAL),
197 maskval(revision, MC34708_REVISION_FIN),
198 maskval(revision, MC34708_REVISION_FAB));
199}
200
Uwe Kleine-Königcd0f34b2012-07-12 09:57:52 +0000201/* These are only exported for mc13xxx-i2c and mc13xxx-spi */
202struct mc13xxx_variant mc13xxx_variant_mc13783 = {
203 .name = "mc13783",
204 .print_revision = mc13xxx_print_revision,
205};
206EXPORT_SYMBOL_GPL(mc13xxx_variant_mc13783);
207
208struct mc13xxx_variant mc13xxx_variant_mc13892 = {
209 .name = "mc13892",
210 .print_revision = mc13xxx_print_revision,
211};
212EXPORT_SYMBOL_GPL(mc13xxx_variant_mc13892);
213
Uwe Kleine-König0312e022012-07-12 09:57:53 +0000214struct mc13xxx_variant mc13xxx_variant_mc34708 = {
215 .name = "mc34708",
216 .print_revision = mc34708_print_revision,
217};
218EXPORT_SYMBOL_GPL(mc13xxx_variant_mc34708);
219
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200220static const char *mc13xxx_get_chipname(struct mc13xxx *mc13xxx)
221{
Uwe Kleine-Königcd0f34b2012-07-12 09:57:52 +0000222 return mc13xxx->variant->name;
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200223}
224
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200225int mc13xxx_get_flags(struct mc13xxx *mc13xxx)
226{
Shawn Guo876989d2011-12-12 18:52:57 +0100227 return mc13xxx->flags;
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200228}
229EXPORT_SYMBOL(mc13xxx_get_flags);
230
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200231#define MC13XXX_ADC1_CHAN0_SHIFT 5
232#define MC13XXX_ADC1_CHAN1_SHIFT 8
Michael Thalmeier1039d762012-02-20 12:18:13 +0100233#define MC13783_ADC1_ATO_SHIFT 11
234#define MC13783_ADC1_ATOX (1 << 19)
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200235
236struct mc13xxx_adcdone_data {
237 struct mc13xxx *mc13xxx;
238 struct completion done;
239};
240
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200241static irqreturn_t mc13xxx_handler_adcdone(int irq, void *data)
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200242{
243 struct mc13xxx_adcdone_data *adcdone_data = data;
244
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200245 complete_all(&adcdone_data->done);
246
247 return IRQ_HANDLED;
248}
249
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200250#define MC13XXX_ADC_WORKING (1 << 0)
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200251
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200252int mc13xxx_adc_do_conversion(struct mc13xxx *mc13xxx, unsigned int mode,
Michael Thalmeier1039d762012-02-20 12:18:13 +0100253 unsigned int channel, u8 ato, bool atox,
254 unsigned int *sample)
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200255{
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200256 u32 adc0, adc1, old_adc0;
257 int i, ret;
258 struct mc13xxx_adcdone_data adcdone_data = {
259 .mc13xxx = mc13xxx,
260 };
261 init_completion(&adcdone_data.done);
262
Marc Reilly5006fe52012-05-01 12:26:46 +0200263 dev_dbg(mc13xxx->dev, "%s\n", __func__);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200264
265 mc13xxx_lock(mc13xxx);
266
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200267 if (mc13xxx->adcflags & MC13XXX_ADC_WORKING) {
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200268 ret = -EBUSY;
269 goto out;
270 }
271
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200272 mc13xxx->adcflags |= MC13XXX_ADC_WORKING;
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200273
Kangjie Lu9e289892018-12-20 15:12:11 -0600274 ret = mc13xxx_reg_read(mc13xxx, MC13XXX_ADC0, &old_adc0);
275 if (ret)
276 goto out;
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200277
Fabio Estevam551434392018-08-28 17:02:40 -0300278 adc0 = MC13XXX_ADC0_ADINC1 | MC13XXX_ADC0_ADINC2 |
279 MC13XXX_ADC0_CHRGRAWDIV;
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200280 adc1 = MC13XXX_ADC1_ADEN | MC13XXX_ADC1_ADTRIGIGN | MC13XXX_ADC1_ASC;
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200281
Andrey Gusakoved645cc2018-03-27 17:19:49 +0300282 /*
283 * Channels mapped through ADIN7:
284 * 7 - General purpose ADIN7
285 * 16 - UID
286 * 17 - Die temperature
287 */
288 if (channel > 7 && channel < 16) {
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200289 adc1 |= MC13XXX_ADC1_ADSEL;
Andrey Gusakoved645cc2018-03-27 17:19:49 +0300290 } else if (channel == 16) {
291 adc0 |= MC13XXX_ADC0_ADIN7SEL_UID;
292 channel = 7;
293 } else if (channel == 17) {
294 adc0 |= MC13XXX_ADC0_ADIN7SEL_DIE;
295 channel = 7;
296 }
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200297
298 switch (mode) {
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200299 case MC13XXX_ADC_MODE_TS:
300 adc0 |= MC13XXX_ADC0_ADREFEN | MC13XXX_ADC0_TSMOD0 |
301 MC13XXX_ADC0_TSMOD1;
302 adc1 |= 4 << MC13XXX_ADC1_CHAN1_SHIFT;
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200303 break;
304
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200305 case MC13XXX_ADC_MODE_SINGLE_CHAN:
Robin van der Gracht21618912011-11-29 12:09:03 +0100306 adc0 |= old_adc0 & MC13XXX_ADC0_CONFIG_MASK;
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200307 adc1 |= (channel & 0x7) << MC13XXX_ADC1_CHAN0_SHIFT;
308 adc1 |= MC13XXX_ADC1_RAND;
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200309 break;
310
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200311 case MC13XXX_ADC_MODE_MULT_CHAN:
Robin van der Gracht21618912011-11-29 12:09:03 +0100312 adc0 |= old_adc0 & MC13XXX_ADC0_CONFIG_MASK;
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200313 adc1 |= 4 << MC13XXX_ADC1_CHAN1_SHIFT;
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200314 break;
315
316 default:
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200317 mc13xxx_unlock(mc13xxx);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200318 return -EINVAL;
319 }
320
Michael Thalmeier1039d762012-02-20 12:18:13 +0100321 adc1 |= ato << MC13783_ADC1_ATO_SHIFT;
322 if (atox)
323 adc1 |= MC13783_ADC1_ATOX;
Marc Reilly5006fe52012-05-01 12:26:46 +0200324
325 dev_dbg(mc13xxx->dev, "%s: request irq\n", __func__);
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200326 mc13xxx_irq_request(mc13xxx, MC13XXX_IRQ_ADCDONE,
327 mc13xxx_handler_adcdone, __func__, &adcdone_data);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200328
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200329 mc13xxx_reg_write(mc13xxx, MC13XXX_ADC0, adc0);
330 mc13xxx_reg_write(mc13xxx, MC13XXX_ADC1, adc1);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200331
332 mc13xxx_unlock(mc13xxx);
333
334 ret = wait_for_completion_interruptible_timeout(&adcdone_data.done, HZ);
335
336 if (!ret)
337 ret = -ETIMEDOUT;
338
339 mc13xxx_lock(mc13xxx);
340
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200341 mc13xxx_irq_free(mc13xxx, MC13XXX_IRQ_ADCDONE, &adcdone_data);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200342
343 if (ret > 0)
344 for (i = 0; i < 4; ++i) {
345 ret = mc13xxx_reg_read(mc13xxx,
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200346 MC13XXX_ADC2, &sample[i]);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200347 if (ret)
348 break;
349 }
350
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200351 if (mode == MC13XXX_ADC_MODE_TS)
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200352 /* restore TSMOD */
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200353 mc13xxx_reg_write(mc13xxx, MC13XXX_ADC0, old_adc0);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200354
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200355 mc13xxx->adcflags &= ~MC13XXX_ADC_WORKING;
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200356out:
357 mc13xxx_unlock(mc13xxx);
358
359 return ret;
360}
Uwe Kleine-Königfec316d2011-08-24 15:28:21 +0200361EXPORT_SYMBOL_GPL(mc13xxx_adc_do_conversion);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200362
363static int mc13xxx_add_subdevice_pdata(struct mc13xxx *mc13xxx,
Samuel Ortizc8a03c92011-04-08 01:55:01 +0200364 const char *format, void *pdata, size_t pdata_size)
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200365{
366 char buf[30];
367 const char *name = mc13xxx_get_chipname(mc13xxx);
368
369 struct mfd_cell cell = {
Samuel Ortizc8a03c92011-04-08 01:55:01 +0200370 .platform_data = pdata,
371 .pdata_size = pdata_size,
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200372 };
373
374 /* there is no asnprintf in the kernel :-( */
375 if (snprintf(buf, sizeof(buf), format, name) > sizeof(buf))
376 return -E2BIG;
377
378 cell.name = kmemdup(buf, strlen(buf) + 1, GFP_KERNEL);
379 if (!cell.name)
380 return -ENOMEM;
381
Alexander Shiyan10f9eda2014-06-18 21:05:40 +0400382 return mfd_add_devices(mc13xxx->dev, -1, &cell, 1, NULL, 0,
383 regmap_irq_get_domain(mc13xxx->irq_data));
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200384}
385
386static int mc13xxx_add_subdevice(struct mc13xxx *mc13xxx, const char *format)
387{
Samuel Ortizc8a03c92011-04-08 01:55:01 +0200388 return mc13xxx_add_subdevice_pdata(mc13xxx, format, NULL, 0);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200389}
390
Shawn Guo876989d2011-12-12 18:52:57 +0100391#ifdef CONFIG_OF
392static int mc13xxx_probe_flags_dt(struct mc13xxx *mc13xxx)
393{
Marc Reilly91b5e742012-04-01 16:41:37 +1000394 struct device_node *np = mc13xxx->dev->of_node;
Shawn Guo876989d2011-12-12 18:52:57 +0100395
396 if (!np)
397 return -ENODEV;
398
Saurabh Sengar505b9e52015-11-16 14:43:17 +0530399 if (of_property_read_bool(np, "fsl,mc13xxx-uses-adc"))
Shawn Guo876989d2011-12-12 18:52:57 +0100400 mc13xxx->flags |= MC13XXX_USE_ADC;
401
Saurabh Sengar505b9e52015-11-16 14:43:17 +0530402 if (of_property_read_bool(np, "fsl,mc13xxx-uses-codec"))
Shawn Guo876989d2011-12-12 18:52:57 +0100403 mc13xxx->flags |= MC13XXX_USE_CODEC;
404
Saurabh Sengar505b9e52015-11-16 14:43:17 +0530405 if (of_property_read_bool(np, "fsl,mc13xxx-uses-rtc"))
Shawn Guo876989d2011-12-12 18:52:57 +0100406 mc13xxx->flags |= MC13XXX_USE_RTC;
407
Saurabh Sengar505b9e52015-11-16 14:43:17 +0530408 if (of_property_read_bool(np, "fsl,mc13xxx-uses-touch"))
Shawn Guo876989d2011-12-12 18:52:57 +0100409 mc13xxx->flags |= MC13XXX_USE_TOUCHSCREEN;
410
411 return 0;
412}
413#else
414static inline int mc13xxx_probe_flags_dt(struct mc13xxx *mc13xxx)
415{
416 return -ENODEV;
417}
418#endif
419
Alexander Shiyandb9ef442013-12-14 17:03:12 +0400420int mc13xxx_common_init(struct device *dev)
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200421{
Alexander Shiyandb9ef442013-12-14 17:03:12 +0400422 struct mc13xxx_platform_data *pdata = dev_get_platdata(dev);
423 struct mc13xxx *mc13xxx = dev_get_drvdata(dev);
Uwe Kleine-Königcd0f34b2012-07-12 09:57:52 +0000424 u32 revision;
Alexander Shiyan10f9eda2014-06-18 21:05:40 +0400425 int i, ret;
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200426
Alexander Shiyandb9ef442013-12-14 17:03:12 +0400427 mc13xxx->dev = dev;
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200428
Uwe Kleine-Königcd0f34b2012-07-12 09:57:52 +0000429 ret = mc13xxx_reg_read(mc13xxx, MC13XXX_REVISION, &revision);
Marc Reilly5006fe52012-05-01 12:26:46 +0200430 if (ret)
Alexander Shiyandb9ef442013-12-14 17:03:12 +0400431 return ret;
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200432
Uwe Kleine-Königcd0f34b2012-07-12 09:57:52 +0000433 mc13xxx->variant->print_revision(mc13xxx, revision);
434
Markus Pargmann34a49582014-09-08 09:01:11 +0200435 ret = mc13xxx_reg_rmw(mc13xxx, MC13XXX_PWRCTRL,
436 MC13XXX_PWRCTRL_WDIRESET, MC13XXX_PWRCTRL_WDIRESET);
437 if (ret)
438 return ret;
439
Alexander Shiyan10f9eda2014-06-18 21:05:40 +0400440 for (i = 0; i < ARRAY_SIZE(mc13xxx->irqs); i++) {
441 mc13xxx->irqs[i].reg_offset = i / MC13XXX_IRQ_PER_REG;
442 mc13xxx->irqs[i].mask = BIT(i % MC13XXX_IRQ_PER_REG);
443 }
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200444
Alexander Shiyan10f9eda2014-06-18 21:05:40 +0400445 mc13xxx->irq_chip.name = dev_name(dev);
446 mc13xxx->irq_chip.status_base = MC13XXX_IRQSTAT0;
447 mc13xxx->irq_chip.mask_base = MC13XXX_IRQMASK0;
448 mc13xxx->irq_chip.ack_base = MC13XXX_IRQSTAT0;
449 mc13xxx->irq_chip.irq_reg_stride = MC13XXX_IRQSTAT1 - MC13XXX_IRQSTAT0;
450 mc13xxx->irq_chip.init_ack_masked = true;
451 mc13xxx->irq_chip.use_ack = true;
452 mc13xxx->irq_chip.num_regs = MC13XXX_IRQ_REG_CNT;
453 mc13xxx->irq_chip.irqs = mc13xxx->irqs;
454 mc13xxx->irq_chip.num_irqs = ARRAY_SIZE(mc13xxx->irqs);
455
456 ret = regmap_add_irq_chip(mc13xxx->regmap, mc13xxx->irq, IRQF_ONESHOT,
457 0, &mc13xxx->irq_chip, &mc13xxx->irq_data);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200458 if (ret)
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200459 return ret;
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200460
Fabio Estevam48ca9a52014-04-15 19:56:14 -0300461 mutex_init(&mc13xxx->lock);
462
Shawn Guo876989d2011-12-12 18:52:57 +0100463 if (mc13xxx_probe_flags_dt(mc13xxx) < 0 && pdata)
464 mc13xxx->flags = pdata->flags;
465
Shawn Guo876989d2011-12-12 18:52:57 +0100466 if (pdata) {
467 mc13xxx_add_subdevice_pdata(mc13xxx, "%s-regulator",
468 &pdata->regulators, sizeof(pdata->regulators));
Samuel Ortizc8a03c92011-04-08 01:55:01 +0200469 mc13xxx_add_subdevice_pdata(mc13xxx, "%s-led",
470 pdata->leds, sizeof(*pdata->leds));
Philippe Rétornaz30fc7ac2011-09-18 18:10:53 +0200471 mc13xxx_add_subdevice_pdata(mc13xxx, "%s-pwrbutton",
472 pdata->buttons, sizeof(*pdata->buttons));
Alexander Shiyana2ff8452014-05-01 10:48:03 +0400473 if (mc13xxx->flags & MC13XXX_USE_CODEC)
474 mc13xxx_add_subdevice_pdata(mc13xxx, "%s-codec",
475 pdata->codec, sizeof(*pdata->codec));
476 if (mc13xxx->flags & MC13XXX_USE_TOUCHSCREEN)
477 mc13xxx_add_subdevice_pdata(mc13xxx, "%s-ts",
478 &pdata->touch, sizeof(pdata->touch));
Shawn Guo876989d2011-12-12 18:52:57 +0100479 } else {
480 mc13xxx_add_subdevice(mc13xxx, "%s-regulator");
481 mc13xxx_add_subdevice(mc13xxx, "%s-led");
482 mc13xxx_add_subdevice(mc13xxx, "%s-pwrbutton");
Alexander Shiyana2ff8452014-05-01 10:48:03 +0400483 if (mc13xxx->flags & MC13XXX_USE_CODEC)
484 mc13xxx_add_subdevice(mc13xxx, "%s-codec");
485 if (mc13xxx->flags & MC13XXX_USE_TOUCHSCREEN)
486 mc13xxx_add_subdevice(mc13xxx, "%s-ts");
Shawn Guo876989d2011-12-12 18:52:57 +0100487 }
Philippe Rétornaz30fc7ac2011-09-18 18:10:53 +0200488
Alexander Shiyan10f9eda2014-06-18 21:05:40 +0400489 if (mc13xxx->flags & MC13XXX_USE_ADC)
490 mc13xxx_add_subdevice(mc13xxx, "%s-adc");
491
492 if (mc13xxx->flags & MC13XXX_USE_RTC)
493 mc13xxx_add_subdevice(mc13xxx, "%s-rtc");
494
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200495 return 0;
496}
Marc Reillya0c7c1d2012-04-01 16:41:38 +1000497EXPORT_SYMBOL_GPL(mc13xxx_common_init);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200498
Alexander Shiyandb9ef442013-12-14 17:03:12 +0400499int mc13xxx_common_exit(struct device *dev)
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200500{
Alexander Shiyandb9ef442013-12-14 17:03:12 +0400501 struct mc13xxx *mc13xxx = dev_get_drvdata(dev);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200502
Alexander Shiyandb9ef442013-12-14 17:03:12 +0400503 mfd_remove_devices(dev);
Alexander Shiyan10f9eda2014-06-18 21:05:40 +0400504 regmap_del_irq_chip(mc13xxx->irq, mc13xxx->irq_data);
Alexander Shiyandb9ef442013-12-14 17:03:12 +0400505 mutex_destroy(&mc13xxx->lock);
506
507 return 0;
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200508}
Alexander Shiyandb9ef442013-12-14 17:03:12 +0400509EXPORT_SYMBOL_GPL(mc13xxx_common_exit);
Uwe Kleine-König8e005932010-09-28 16:37:20 +0200510
511MODULE_DESCRIPTION("Core driver for Freescale MC13XXX PMIC");
512MODULE_AUTHOR("Uwe Kleine-Koenig <u.kleine-koenig@pengutronix.de>");
513MODULE_LICENSE("GPL v2");