blob: 3269a9990b2422cfa05de0aa30dd163fc1b5f397 [file] [log] [blame]
Haojian Zhuangbbd51b12010-01-06 17:04:18 -05001/*
2 * Base driver for Marvell 88PM8607
3 *
4 * Copyright (C) 2009 Marvell International Ltd.
Lee Jones2e578482014-05-08 12:28:37 +01005 *
6 * Author: Haojian Zhuang <haojian.zhuang@marvell.com>
Haojian Zhuangbbd51b12010-01-06 17:04:18 -05007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13#include <linux/kernel.h>
14#include <linux/module.h>
Haojian Zhuangf2f218c2012-09-17 12:19:08 +080015#include <linux/err.h>
Haojian Zhuang5c42e8c2009-12-15 16:01:47 -050016#include <linux/i2c.h>
Haojian Zhuang2afa62e2010-02-08 05:02:00 -050017#include <linux/irq.h>
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050018#include <linux/interrupt.h>
Haojian Zhuang837c8292012-10-02 10:23:20 +080019#include <linux/irqdomain.h>
Haojian Zhuang2e57d562012-09-21 18:06:52 +080020#include <linux/of.h>
21#include <linux/of_platform.h>
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050022#include <linux/platform_device.h>
Haojian Zhuangf2f218c2012-09-17 12:19:08 +080023#include <linux/regmap.h>
24#include <linux/slab.h>
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050025#include <linux/mfd/core.h>
Haojian Zhuang53dbab72010-01-08 06:01:24 -050026#include <linux/mfd/88pm860x.h>
Haojian Zhuang22aad002011-03-07 23:43:11 +080027#include <linux/regulator/machine.h>
Jett.Zhoua830d282012-07-27 16:27:16 +080028#include <linux/power/charger-manager.h>
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050029
Haojian Zhuang2afa62e2010-02-08 05:02:00 -050030#define INT_STATUS_NUM 3
31
Bill Pembertona9e9ce42012-11-19 13:24:21 -050032static struct resource bk0_resources[] = {
Haojian Zhuanga6ccdcd2012-08-08 23:17:26 +080033 {2, 2, "duty cycle", IORESOURCE_REG, },
34 {3, 3, "always on", IORESOURCE_REG, },
35 {3, 3, "current", IORESOURCE_REG, },
36};
Bill Pembertona9e9ce42012-11-19 13:24:21 -050037static struct resource bk1_resources[] = {
Haojian Zhuanga6ccdcd2012-08-08 23:17:26 +080038 {4, 4, "duty cycle", IORESOURCE_REG, },
39 {5, 5, "always on", IORESOURCE_REG, },
40 {5, 5, "current", IORESOURCE_REG, },
41};
Bill Pembertona9e9ce42012-11-19 13:24:21 -050042static struct resource bk2_resources[] = {
Haojian Zhuanga6ccdcd2012-08-08 23:17:26 +080043 {6, 6, "duty cycle", IORESOURCE_REG, },
44 {7, 7, "always on", IORESOURCE_REG, },
45 {5, 5, "current", IORESOURCE_REG, },
Haojian Zhuanga16122b2009-12-15 16:04:36 -050046};
Haojian Zhuangadb70482011-03-07 23:43:09 +080047
Bill Pembertona9e9ce42012-11-19 13:24:21 -050048static struct resource led0_resources[] = {
Haojian Zhuang894fc8f2012-08-08 23:17:27 +080049 /* RGB1 Red LED */
50 {0xd, 0xd, "control", IORESOURCE_REG, },
51 {0xc, 0xc, "blink", IORESOURCE_REG, },
52};
Bill Pembertona9e9ce42012-11-19 13:24:21 -050053static struct resource led1_resources[] = {
Haojian Zhuang894fc8f2012-08-08 23:17:27 +080054 /* RGB1 Green LED */
55 {0xe, 0xe, "control", IORESOURCE_REG, },
56 {0xc, 0xc, "blink", IORESOURCE_REG, },
57};
Bill Pembertona9e9ce42012-11-19 13:24:21 -050058static struct resource led2_resources[] = {
Haojian Zhuang894fc8f2012-08-08 23:17:27 +080059 /* RGB1 Blue LED */
60 {0xf, 0xf, "control", IORESOURCE_REG, },
61 {0xc, 0xc, "blink", IORESOURCE_REG, },
62};
Bill Pembertona9e9ce42012-11-19 13:24:21 -050063static struct resource led3_resources[] = {
Haojian Zhuang894fc8f2012-08-08 23:17:27 +080064 /* RGB2 Red LED */
65 {0x9, 0x9, "control", IORESOURCE_REG, },
66 {0x8, 0x8, "blink", IORESOURCE_REG, },
67};
Bill Pembertona9e9ce42012-11-19 13:24:21 -050068static struct resource led4_resources[] = {
Haojian Zhuang894fc8f2012-08-08 23:17:27 +080069 /* RGB2 Green LED */
70 {0xa, 0xa, "control", IORESOURCE_REG, },
71 {0x8, 0x8, "blink", IORESOURCE_REG, },
72};
Bill Pembertona9e9ce42012-11-19 13:24:21 -050073static struct resource led5_resources[] = {
Haojian Zhuang894fc8f2012-08-08 23:17:27 +080074 /* RGB2 Blue LED */
75 {0xb, 0xb, "control", IORESOURCE_REG, },
76 {0x8, 0x8, "blink", IORESOURCE_REG, },
Haojian Zhuang3154c342011-03-07 23:43:10 +080077};
78
Bill Pembertona9e9ce42012-11-19 13:24:21 -050079static struct resource buck1_resources[] = {
Haojian Zhuanga70abac2012-08-08 23:17:28 +080080 {0x24, 0x24, "buck set", IORESOURCE_REG, },
81};
Bill Pembertona9e9ce42012-11-19 13:24:21 -050082static struct resource buck2_resources[] = {
Haojian Zhuanga70abac2012-08-08 23:17:28 +080083 {0x25, 0x25, "buck set", IORESOURCE_REG, },
84};
Bill Pembertona9e9ce42012-11-19 13:24:21 -050085static struct resource buck3_resources[] = {
Haojian Zhuanga70abac2012-08-08 23:17:28 +080086 {0x26, 0x26, "buck set", IORESOURCE_REG, },
87};
Bill Pembertona9e9ce42012-11-19 13:24:21 -050088static struct resource ldo1_resources[] = {
Haojian Zhuanga70abac2012-08-08 23:17:28 +080089 {0x10, 0x10, "ldo set", IORESOURCE_REG, },
90};
Bill Pembertona9e9ce42012-11-19 13:24:21 -050091static struct resource ldo2_resources[] = {
Haojian Zhuanga70abac2012-08-08 23:17:28 +080092 {0x11, 0x11, "ldo set", IORESOURCE_REG, },
93};
Bill Pembertona9e9ce42012-11-19 13:24:21 -050094static struct resource ldo3_resources[] = {
Haojian Zhuanga70abac2012-08-08 23:17:28 +080095 {0x12, 0x12, "ldo set", IORESOURCE_REG, },
96};
Bill Pembertona9e9ce42012-11-19 13:24:21 -050097static struct resource ldo4_resources[] = {
Haojian Zhuanga70abac2012-08-08 23:17:28 +080098 {0x13, 0x13, "ldo set", IORESOURCE_REG, },
99};
Bill Pembertona9e9ce42012-11-19 13:24:21 -0500100static struct resource ldo5_resources[] = {
Haojian Zhuanga70abac2012-08-08 23:17:28 +0800101 {0x14, 0x14, "ldo set", IORESOURCE_REG, },
102};
Bill Pembertona9e9ce42012-11-19 13:24:21 -0500103static struct resource ldo6_resources[] = {
Haojian Zhuanga70abac2012-08-08 23:17:28 +0800104 {0x15, 0x15, "ldo set", IORESOURCE_REG, },
105};
Bill Pembertona9e9ce42012-11-19 13:24:21 -0500106static struct resource ldo7_resources[] = {
Haojian Zhuanga70abac2012-08-08 23:17:28 +0800107 {0x16, 0x16, "ldo set", IORESOURCE_REG, },
108};
Bill Pembertona9e9ce42012-11-19 13:24:21 -0500109static struct resource ldo8_resources[] = {
Haojian Zhuanga70abac2012-08-08 23:17:28 +0800110 {0x17, 0x17, "ldo set", IORESOURCE_REG, },
111};
Bill Pembertona9e9ce42012-11-19 13:24:21 -0500112static struct resource ldo9_resources[] = {
Haojian Zhuanga70abac2012-08-08 23:17:28 +0800113 {0x18, 0x18, "ldo set", IORESOURCE_REG, },
114};
Bill Pembertona9e9ce42012-11-19 13:24:21 -0500115static struct resource ldo10_resources[] = {
Haojian Zhuanga70abac2012-08-08 23:17:28 +0800116 {0x19, 0x19, "ldo set", IORESOURCE_REG, },
117};
Bill Pembertona9e9ce42012-11-19 13:24:21 -0500118static struct resource ldo12_resources[] = {
Haojian Zhuanga70abac2012-08-08 23:17:28 +0800119 {0x1a, 0x1a, "ldo set", IORESOURCE_REG, },
120};
Bill Pembertona9e9ce42012-11-19 13:24:21 -0500121static struct resource ldo_vibrator_resources[] = {
Haojian Zhuanga70abac2012-08-08 23:17:28 +0800122 {0x28, 0x28, "ldo set", IORESOURCE_REG, },
123};
Bill Pembertona9e9ce42012-11-19 13:24:21 -0500124static struct resource ldo14_resources[] = {
Haojian Zhuanga70abac2012-08-08 23:17:28 +0800125 {0x1b, 0x1b, "ldo set", IORESOURCE_REG, },
Haojian Zhuang22aad002011-03-07 23:43:11 +0800126};
127
Bill Pembertona9e9ce42012-11-19 13:24:21 -0500128static struct resource touch_resources[] = {
Haojian Zhuangc9f560b2011-03-07 23:43:12 +0800129 {PM8607_IRQ_PEN, PM8607_IRQ_PEN, "touch", IORESOURCE_IRQ,},
130};
131
Bill Pembertona9e9ce42012-11-19 13:24:21 -0500132static struct resource onkey_resources[] = {
Haojian Zhuangc9f560b2011-03-07 23:43:12 +0800133 {PM8607_IRQ_ONKEY, PM8607_IRQ_ONKEY, "onkey", IORESOURCE_IRQ,},
134};
135
Bill Pembertona9e9ce42012-11-19 13:24:21 -0500136static struct resource codec_resources[] = {
Haojian Zhuangc9f560b2011-03-07 23:43:12 +0800137 /* Headset microphone insertion or removal */
138 {PM8607_IRQ_MICIN, PM8607_IRQ_MICIN, "micin", IORESOURCE_IRQ,},
139 /* Hook-switch press or release */
140 {PM8607_IRQ_HOOK, PM8607_IRQ_HOOK, "hook", IORESOURCE_IRQ,},
141 /* Headset insertion or removal */
142 {PM8607_IRQ_HEADSET, PM8607_IRQ_HEADSET, "headset", IORESOURCE_IRQ,},
143 /* Audio short */
Lee Jones2e578482014-05-08 12:28:37 +0100144 {PM8607_IRQ_AUDIO_SHORT, PM8607_IRQ_AUDIO_SHORT, "audio-short",
145 IORESOURCE_IRQ,},
Haojian Zhuangc9f560b2011-03-07 23:43:12 +0800146};
147
Bill Pembertona9e9ce42012-11-19 13:24:21 -0500148static struct resource battery_resources[] = {
Haojian Zhuangc9f560b2011-03-07 23:43:12 +0800149 {PM8607_IRQ_CC, PM8607_IRQ_CC, "columb counter", IORESOURCE_IRQ,},
150 {PM8607_IRQ_BAT, PM8607_IRQ_BAT, "battery", IORESOURCE_IRQ,},
151};
152
Bill Pembertona9e9ce42012-11-19 13:24:21 -0500153static struct resource charger_resources[] = {
Haojian Zhuangc9f560b2011-03-07 23:43:12 +0800154 {PM8607_IRQ_CHG, PM8607_IRQ_CHG, "charger detect", IORESOURCE_IRQ,},
Lee Jones2e578482014-05-08 12:28:37 +0100155 {PM8607_IRQ_CHG_DONE, PM8607_IRQ_CHG_DONE, "charging done",
156 IORESOURCE_IRQ,},
157 {PM8607_IRQ_CHG_FAIL, PM8607_IRQ_CHG_FAIL, "charging timeout",
158 IORESOURCE_IRQ,},
159 {PM8607_IRQ_CHG_FAULT, PM8607_IRQ_CHG_FAULT, "charging fault",
160 IORESOURCE_IRQ,},
161 {PM8607_IRQ_GPADC1, PM8607_IRQ_GPADC1, "battery temperature",
162 IORESOURCE_IRQ,},
Haojian Zhuangc9f560b2011-03-07 23:43:12 +0800163 {PM8607_IRQ_VBAT, PM8607_IRQ_VBAT, "battery voltage", IORESOURCE_IRQ,},
164 {PM8607_IRQ_VCHG, PM8607_IRQ_VCHG, "vchg voltage", IORESOURCE_IRQ,},
165};
166
Bill Pembertona9e9ce42012-11-19 13:24:21 -0500167static struct resource rtc_resources[] = {
Mark Brown02367022012-08-07 19:42:48 +0100168 {PM8607_IRQ_RTC, PM8607_IRQ_RTC, "rtc", IORESOURCE_IRQ,},
Haojian Zhuang008b3042011-05-06 17:21:20 +0800169};
170
Bill Pembertona9e9ce42012-11-19 13:24:21 -0500171static struct mfd_cell bk_devs[] = {
Haojian Zhuanga6ccdcd2012-08-08 23:17:26 +0800172 {
173 .name = "88pm860x-backlight",
174 .id = 0,
175 .num_resources = ARRAY_SIZE(bk0_resources),
176 .resources = bk0_resources,
177 }, {
178 .name = "88pm860x-backlight",
179 .id = 1,
180 .num_resources = ARRAY_SIZE(bk1_resources),
181 .resources = bk1_resources,
182 }, {
183 .name = "88pm860x-backlight",
184 .id = 2,
185 .num_resources = ARRAY_SIZE(bk2_resources),
186 .resources = bk2_resources,
187 },
Haojian Zhuangadb70482011-03-07 23:43:09 +0800188};
189
Bill Pembertona9e9ce42012-11-19 13:24:21 -0500190static struct mfd_cell led_devs[] = {
Haojian Zhuang894fc8f2012-08-08 23:17:27 +0800191 {
192 .name = "88pm860x-led",
193 .id = 0,
194 .num_resources = ARRAY_SIZE(led0_resources),
195 .resources = led0_resources,
196 }, {
197 .name = "88pm860x-led",
198 .id = 1,
199 .num_resources = ARRAY_SIZE(led1_resources),
200 .resources = led1_resources,
201 }, {
202 .name = "88pm860x-led",
203 .id = 2,
204 .num_resources = ARRAY_SIZE(led2_resources),
205 .resources = led2_resources,
206 }, {
207 .name = "88pm860x-led",
208 .id = 3,
209 .num_resources = ARRAY_SIZE(led3_resources),
210 .resources = led3_resources,
211 }, {
212 .name = "88pm860x-led",
213 .id = 4,
214 .num_resources = ARRAY_SIZE(led4_resources),
215 .resources = led4_resources,
216 }, {
217 .name = "88pm860x-led",
218 .id = 5,
219 .num_resources = ARRAY_SIZE(led5_resources),
220 .resources = led5_resources,
221 },
Haojian Zhuang3154c342011-03-07 23:43:10 +0800222};
223
Bill Pembertona9e9ce42012-11-19 13:24:21 -0500224static struct mfd_cell reg_devs[] = {
Haojian Zhuanga70abac2012-08-08 23:17:28 +0800225 {
226 .name = "88pm860x-regulator",
227 .id = 0,
228 .num_resources = ARRAY_SIZE(buck1_resources),
229 .resources = buck1_resources,
230 }, {
231 .name = "88pm860x-regulator",
232 .id = 1,
233 .num_resources = ARRAY_SIZE(buck2_resources),
234 .resources = buck2_resources,
235 }, {
236 .name = "88pm860x-regulator",
237 .id = 2,
238 .num_resources = ARRAY_SIZE(buck3_resources),
239 .resources = buck3_resources,
240 }, {
241 .name = "88pm860x-regulator",
242 .id = 3,
243 .num_resources = ARRAY_SIZE(ldo1_resources),
244 .resources = ldo1_resources,
245 }, {
246 .name = "88pm860x-regulator",
247 .id = 4,
248 .num_resources = ARRAY_SIZE(ldo2_resources),
249 .resources = ldo2_resources,
250 }, {
251 .name = "88pm860x-regulator",
252 .id = 5,
253 .num_resources = ARRAY_SIZE(ldo3_resources),
254 .resources = ldo3_resources,
255 }, {
256 .name = "88pm860x-regulator",
257 .id = 6,
258 .num_resources = ARRAY_SIZE(ldo4_resources),
259 .resources = ldo4_resources,
260 }, {
261 .name = "88pm860x-regulator",
262 .id = 7,
263 .num_resources = ARRAY_SIZE(ldo5_resources),
264 .resources = ldo5_resources,
265 }, {
266 .name = "88pm860x-regulator",
267 .id = 8,
268 .num_resources = ARRAY_SIZE(ldo6_resources),
269 .resources = ldo6_resources,
270 }, {
271 .name = "88pm860x-regulator",
272 .id = 9,
273 .num_resources = ARRAY_SIZE(ldo7_resources),
274 .resources = ldo7_resources,
275 }, {
276 .name = "88pm860x-regulator",
277 .id = 10,
278 .num_resources = ARRAY_SIZE(ldo8_resources),
279 .resources = ldo8_resources,
280 }, {
281 .name = "88pm860x-regulator",
282 .id = 11,
283 .num_resources = ARRAY_SIZE(ldo9_resources),
284 .resources = ldo9_resources,
285 }, {
286 .name = "88pm860x-regulator",
287 .id = 12,
288 .num_resources = ARRAY_SIZE(ldo10_resources),
289 .resources = ldo10_resources,
290 }, {
291 .name = "88pm860x-regulator",
292 .id = 13,
293 .num_resources = ARRAY_SIZE(ldo12_resources),
294 .resources = ldo12_resources,
295 }, {
296 .name = "88pm860x-regulator",
297 .id = 14,
298 .num_resources = ARRAY_SIZE(ldo_vibrator_resources),
299 .resources = ldo_vibrator_resources,
300 }, {
301 .name = "88pm860x-regulator",
302 .id = 15,
303 .num_resources = ARRAY_SIZE(ldo14_resources),
304 .resources = ldo14_resources,
305 },
Haojian Zhuang22aad002011-03-07 23:43:11 +0800306};
307
Haojian Zhuanga5156f12011-04-26 11:06:22 +0200308static struct mfd_cell touch_devs[] = {
Haojian Zhuangc9f560b2011-03-07 23:43:12 +0800309 {"88pm860x-touch", -1,},
Haojian Zhuanga16122b2009-12-15 16:04:36 -0500310};
311
Haojian Zhuanga5156f12011-04-26 11:06:22 +0200312static struct mfd_cell onkey_devs[] = {
Haojian Zhuangc9f560b2011-03-07 23:43:12 +0800313 {"88pm860x-onkey", -1,},
Haojian Zhuanga16122b2009-12-15 16:04:36 -0500314};
Haojian Zhuangbbd51b12010-01-06 17:04:18 -0500315
Haojian Zhuanga5156f12011-04-26 11:06:22 +0200316static struct mfd_cell codec_devs[] = {
Haojian Zhuangc9f560b2011-03-07 23:43:12 +0800317 {"88pm860x-codec", -1,},
Haojian Zhuang2afa62e2010-02-08 05:02:00 -0500318};
319
Jett.Zhou2573f6d2012-07-06 10:59:58 +0800320static struct regulator_consumer_supply preg_supply[] = {
321 REGULATOR_SUPPLY("preg", "charger-manager"),
322};
323
324static struct regulator_init_data preg_init_data = {
325 .num_consumer_supplies = ARRAY_SIZE(preg_supply),
326 .consumer_supplies = &preg_supply[0],
327};
328
Anton Vorontsovf1ade352012-08-22 20:31:33 -0700329static struct charger_regulator chg_desc_regulator_data[] = {
330 { .regulator_name = "preg", },
Jett.Zhoua830d282012-07-27 16:27:16 +0800331};
332
Haojian Zhuang2afa62e2010-02-08 05:02:00 -0500333static struct mfd_cell power_devs[] = {
Haojian Zhuangc9f560b2011-03-07 23:43:12 +0800334 {"88pm860x-battery", -1,},
335 {"88pm860x-charger", -1,},
Jett.Zhou2573f6d2012-07-06 10:59:58 +0800336 {"88pm860x-preg", -1,},
Jett.Zhoua830d282012-07-27 16:27:16 +0800337 {"charger-manager", -1,},
Haojian Zhuang2afa62e2010-02-08 05:02:00 -0500338};
339
Haojian Zhuang008b3042011-05-06 17:21:20 +0800340static struct mfd_cell rtc_devs[] = {
341 {"88pm860x-rtc", -1,},
342};
343
Haojian Zhuang2c36af72010-08-12 11:59:33 +0800344
Haojian Zhuang2afa62e2010-02-08 05:02:00 -0500345struct pm860x_irq_data {
346 int reg;
347 int mask_reg;
348 int enable; /* enable or not */
349 int offs; /* bit offset in mask register */
350};
Haojian Zhuang5c42e8c2009-12-15 16:01:47 -0500351
Haojian Zhuang2afa62e2010-02-08 05:02:00 -0500352static struct pm860x_irq_data pm860x_irqs[] = {
353 [PM8607_IRQ_ONKEY] = {
354 .reg = PM8607_INT_STATUS1,
355 .mask_reg = PM8607_INT_MASK_1,
356 .offs = 1 << 0,
357 },
358 [PM8607_IRQ_EXTON] = {
359 .reg = PM8607_INT_STATUS1,
360 .mask_reg = PM8607_INT_MASK_1,
361 .offs = 1 << 1,
362 },
363 [PM8607_IRQ_CHG] = {
364 .reg = PM8607_INT_STATUS1,
365 .mask_reg = PM8607_INT_MASK_1,
366 .offs = 1 << 2,
367 },
368 [PM8607_IRQ_BAT] = {
369 .reg = PM8607_INT_STATUS1,
370 .mask_reg = PM8607_INT_MASK_1,
371 .offs = 1 << 3,
372 },
373 [PM8607_IRQ_RTC] = {
374 .reg = PM8607_INT_STATUS1,
375 .mask_reg = PM8607_INT_MASK_1,
376 .offs = 1 << 4,
377 },
378 [PM8607_IRQ_CC] = {
379 .reg = PM8607_INT_STATUS1,
380 .mask_reg = PM8607_INT_MASK_1,
381 .offs = 1 << 5,
382 },
383 [PM8607_IRQ_VBAT] = {
384 .reg = PM8607_INT_STATUS2,
385 .mask_reg = PM8607_INT_MASK_2,
386 .offs = 1 << 0,
387 },
388 [PM8607_IRQ_VCHG] = {
389 .reg = PM8607_INT_STATUS2,
390 .mask_reg = PM8607_INT_MASK_2,
391 .offs = 1 << 1,
392 },
393 [PM8607_IRQ_VSYS] = {
394 .reg = PM8607_INT_STATUS2,
395 .mask_reg = PM8607_INT_MASK_2,
396 .offs = 1 << 2,
397 },
398 [PM8607_IRQ_TINT] = {
399 .reg = PM8607_INT_STATUS2,
400 .mask_reg = PM8607_INT_MASK_2,
401 .offs = 1 << 3,
402 },
403 [PM8607_IRQ_GPADC0] = {
404 .reg = PM8607_INT_STATUS2,
405 .mask_reg = PM8607_INT_MASK_2,
406 .offs = 1 << 4,
407 },
408 [PM8607_IRQ_GPADC1] = {
409 .reg = PM8607_INT_STATUS2,
410 .mask_reg = PM8607_INT_MASK_2,
411 .offs = 1 << 5,
412 },
413 [PM8607_IRQ_GPADC2] = {
414 .reg = PM8607_INT_STATUS2,
415 .mask_reg = PM8607_INT_MASK_2,
416 .offs = 1 << 6,
417 },
418 [PM8607_IRQ_GPADC3] = {
419 .reg = PM8607_INT_STATUS2,
420 .mask_reg = PM8607_INT_MASK_2,
421 .offs = 1 << 7,
422 },
423 [PM8607_IRQ_AUDIO_SHORT] = {
424 .reg = PM8607_INT_STATUS3,
425 .mask_reg = PM8607_INT_MASK_3,
426 .offs = 1 << 0,
427 },
428 [PM8607_IRQ_PEN] = {
429 .reg = PM8607_INT_STATUS3,
430 .mask_reg = PM8607_INT_MASK_3,
431 .offs = 1 << 1,
432 },
433 [PM8607_IRQ_HEADSET] = {
434 .reg = PM8607_INT_STATUS3,
435 .mask_reg = PM8607_INT_MASK_3,
436 .offs = 1 << 2,
437 },
438 [PM8607_IRQ_HOOK] = {
439 .reg = PM8607_INT_STATUS3,
440 .mask_reg = PM8607_INT_MASK_3,
441 .offs = 1 << 3,
442 },
443 [PM8607_IRQ_MICIN] = {
444 .reg = PM8607_INT_STATUS3,
445 .mask_reg = PM8607_INT_MASK_3,
446 .offs = 1 << 4,
447 },
448 [PM8607_IRQ_CHG_FAIL] = {
449 .reg = PM8607_INT_STATUS3,
450 .mask_reg = PM8607_INT_MASK_3,
451 .offs = 1 << 5,
452 },
453 [PM8607_IRQ_CHG_DONE] = {
454 .reg = PM8607_INT_STATUS3,
455 .mask_reg = PM8607_INT_MASK_3,
456 .offs = 1 << 6,
457 },
458 [PM8607_IRQ_CHG_FAULT] = {
459 .reg = PM8607_INT_STATUS3,
460 .mask_reg = PM8607_INT_MASK_3,
461 .offs = 1 << 7,
462 },
463};
464
Haojian Zhuang2afa62e2010-02-08 05:02:00 -0500465static irqreturn_t pm860x_irq(int irq, void *data)
Haojian Zhuang5c42e8c2009-12-15 16:01:47 -0500466{
Haojian Zhuang5c42e8c2009-12-15 16:01:47 -0500467 struct pm860x_chip *chip = data;
Haojian Zhuang2afa62e2010-02-08 05:02:00 -0500468 struct pm860x_irq_data *irq_data;
469 struct i2c_client *i2c;
470 int read_reg = -1, value = 0;
471 int i;
Haojian Zhuang5c42e8c2009-12-15 16:01:47 -0500472
Haojian Zhuang2afa62e2010-02-08 05:02:00 -0500473 i2c = (chip->id == CHIP_PM8607) ? chip->client : chip->companion;
474 for (i = 0; i < ARRAY_SIZE(pm860x_irqs); i++) {
475 irq_data = &pm860x_irqs[i];
476 if (read_reg != irq_data->reg) {
477 read_reg = irq_data->reg;
478 value = pm860x_reg_read(i2c, irq_data->reg);
Haojian Zhuang5c42e8c2009-12-15 16:01:47 -0500479 }
Haojian Zhuang2afa62e2010-02-08 05:02:00 -0500480 if (value & irq_data->enable)
481 handle_nested_irq(chip->irq_base + i);
Haojian Zhuang5c42e8c2009-12-15 16:01:47 -0500482 }
Haojian Zhuang5c42e8c2009-12-15 16:01:47 -0500483 return IRQ_HANDLED;
484}
485
Mark Brown49f89d92010-12-11 12:31:31 +0000486static void pm860x_irq_lock(struct irq_data *data)
Haojian Zhuang5c42e8c2009-12-15 16:01:47 -0500487{
Mark Brown49f89d92010-12-11 12:31:31 +0000488 struct pm860x_chip *chip = irq_data_get_irq_chip_data(data);
Haojian Zhuang5c42e8c2009-12-15 16:01:47 -0500489
490 mutex_lock(&chip->irq_lock);
Haojian Zhuang5c42e8c2009-12-15 16:01:47 -0500491}
Haojian Zhuang5c42e8c2009-12-15 16:01:47 -0500492
Mark Brown49f89d92010-12-11 12:31:31 +0000493static void pm860x_irq_sync_unlock(struct irq_data *data)
Haojian Zhuang5c42e8c2009-12-15 16:01:47 -0500494{
Mark Brown49f89d92010-12-11 12:31:31 +0000495 struct pm860x_chip *chip = irq_data_get_irq_chip_data(data);
Haojian Zhuang2afa62e2010-02-08 05:02:00 -0500496 struct pm860x_irq_data *irq_data;
497 struct i2c_client *i2c;
498 static unsigned char cached[3] = {0x0, 0x0, 0x0};
499 unsigned char mask[3];
500 int i;
Haojian Zhuang5c42e8c2009-12-15 16:01:47 -0500501
Haojian Zhuang2afa62e2010-02-08 05:02:00 -0500502 i2c = (chip->id == CHIP_PM8607) ? chip->client : chip->companion;
503 /* Load cached value. In initial, all IRQs are masked */
504 for (i = 0; i < 3; i++)
505 mask[i] = cached[i];
506 for (i = 0; i < ARRAY_SIZE(pm860x_irqs); i++) {
507 irq_data = &pm860x_irqs[i];
508 switch (irq_data->mask_reg) {
509 case PM8607_INT_MASK_1:
510 mask[0] &= ~irq_data->offs;
511 mask[0] |= irq_data->enable;
512 break;
513 case PM8607_INT_MASK_2:
514 mask[1] &= ~irq_data->offs;
515 mask[1] |= irq_data->enable;
516 break;
517 case PM8607_INT_MASK_3:
518 mask[2] &= ~irq_data->offs;
519 mask[2] |= irq_data->enable;
520 break;
521 default:
522 dev_err(chip->dev, "wrong IRQ\n");
523 break;
524 }
525 }
526 /* update mask into registers */
527 for (i = 0; i < 3; i++) {
528 if (mask[i] != cached[i]) {
529 cached[i] = mask[i];
530 pm860x_reg_write(i2c, PM8607_INT_MASK_1 + i, mask[i]);
531 }
532 }
533
Haojian Zhuang5c42e8c2009-12-15 16:01:47 -0500534 mutex_unlock(&chip->irq_lock);
Haojian Zhuang5c42e8c2009-12-15 16:01:47 -0500535}
Haojian Zhuang2afa62e2010-02-08 05:02:00 -0500536
Mark Brown49f89d92010-12-11 12:31:31 +0000537static void pm860x_irq_enable(struct irq_data *data)
Haojian Zhuang2afa62e2010-02-08 05:02:00 -0500538{
Haojian Zhuang837c8292012-10-02 10:23:20 +0800539 pm860x_irqs[data->hwirq].enable = pm860x_irqs[data->hwirq].offs;
Haojian Zhuang2afa62e2010-02-08 05:02:00 -0500540}
541
Mark Brown49f89d92010-12-11 12:31:31 +0000542static void pm860x_irq_disable(struct irq_data *data)
Haojian Zhuang2afa62e2010-02-08 05:02:00 -0500543{
Haojian Zhuang837c8292012-10-02 10:23:20 +0800544 pm860x_irqs[data->hwirq].enable = 0;
Haojian Zhuang2afa62e2010-02-08 05:02:00 -0500545}
546
547static struct irq_chip pm860x_irq_chip = {
548 .name = "88pm860x",
Mark Brown49f89d92010-12-11 12:31:31 +0000549 .irq_bus_lock = pm860x_irq_lock,
550 .irq_bus_sync_unlock = pm860x_irq_sync_unlock,
551 .irq_enable = pm860x_irq_enable,
552 .irq_disable = pm860x_irq_disable,
Haojian Zhuang2afa62e2010-02-08 05:02:00 -0500553};
Haojian Zhuang5c42e8c2009-12-15 16:01:47 -0500554
Haojian Zhuang837c8292012-10-02 10:23:20 +0800555static int pm860x_irq_domain_map(struct irq_domain *d, unsigned int virq,
556 irq_hw_number_t hw)
557{
558 irq_set_chip_data(virq, d->host_data);
559 irq_set_chip_and_handler(virq, &pm860x_irq_chip, handle_edge_irq);
560 irq_set_nested_thread(virq, 1);
Haojian Zhuang837c8292012-10-02 10:23:20 +0800561 irq_set_noprobe(virq);
Haojian Zhuang837c8292012-10-02 10:23:20 +0800562 return 0;
563}
564
Krzysztof Kozlowski7ce7b262015-04-27 21:54:13 +0900565static const struct irq_domain_ops pm860x_irq_domain_ops = {
Haojian Zhuang837c8292012-10-02 10:23:20 +0800566 .map = pm860x_irq_domain_map,
567 .xlate = irq_domain_xlate_onetwocell,
568};
569
Bill Pembertonf791be42012-11-19 13:23:04 -0500570static int device_irq_init(struct pm860x_chip *chip,
Haojian Zhuang5c42e8c2009-12-15 16:01:47 -0500571 struct pm860x_platform_data *pdata)
572{
Lee Jones2e578482014-05-08 12:28:37 +0100573 struct i2c_client *i2c = (chip->id == CHIP_PM8607) ?
574 chip->client : chip->companion;
Haojian Zhuang5c42e8c2009-12-15 16:01:47 -0500575 unsigned char status_buf[INT_STATUS_NUM];
Haojian Zhuang2afa62e2010-02-08 05:02:00 -0500576 unsigned long flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT;
Haojian Zhuang837c8292012-10-02 10:23:20 +0800577 int data, mask, ret = -EINVAL;
578 int nr_irqs, irq_base = -1;
579 struct device_node *node = i2c->dev.of_node;
Haojian Zhuang5c42e8c2009-12-15 16:01:47 -0500580
581 mask = PM8607_B0_MISC1_INV_INT | PM8607_B0_MISC1_INT_CLEAR
582 | PM8607_B0_MISC1_INT_MASK;
583 data = 0;
584 chip->irq_mode = 0;
585 if (pdata && pdata->irq_mode) {
586 /*
587 * irq_mode defines the way of clearing interrupt. If it's 1,
588 * clear IRQ by write. Otherwise, clear it by read.
589 * This control bit is valid from 88PM8607 B0 steping.
590 */
591 data |= PM8607_B0_MISC1_INT_CLEAR;
592 chip->irq_mode = 1;
593 }
594 ret = pm860x_set_bits(i2c, PM8607_B0_MISC1, mask, data);
595 if (ret < 0)
596 goto out;
597
598 /* mask all IRQs */
599 memset(status_buf, 0, INT_STATUS_NUM);
600 ret = pm860x_bulk_write(i2c, PM8607_INT_MASK_1,
601 INT_STATUS_NUM, status_buf);
602 if (ret < 0)
603 goto out;
604
605 if (chip->irq_mode) {
606 /* clear interrupt status by write */
607 memset(status_buf, 0xFF, INT_STATUS_NUM);
608 ret = pm860x_bulk_write(i2c, PM8607_INT_STATUS1,
609 INT_STATUS_NUM, status_buf);
610 } else {
611 /* clear interrupt status by read */
612 ret = pm860x_bulk_read(i2c, PM8607_INT_STATUS1,
613 INT_STATUS_NUM, status_buf);
614 }
615 if (ret < 0)
616 goto out;
617
Haojian Zhuang2afa62e2010-02-08 05:02:00 -0500618 mutex_init(&chip->irq_lock);
Haojian Zhuang837c8292012-10-02 10:23:20 +0800619
620 if (pdata && pdata->irq_base)
621 irq_base = pdata->irq_base;
622 nr_irqs = ARRAY_SIZE(pm860x_irqs);
623 chip->irq_base = irq_alloc_descs(irq_base, 0, nr_irqs, 0);
624 if (chip->irq_base < 0) {
625 dev_err(&i2c->dev, "Failed to allocate interrupts, ret:%d\n",
626 chip->irq_base);
627 ret = -EBUSY;
628 goto out;
629 }
630 irq_domain_add_legacy(node, nr_irqs, chip->irq_base, 0,
631 &pm860x_irq_domain_ops, chip);
Haojian Zhuang2afa62e2010-02-08 05:02:00 -0500632 chip->core_irq = i2c->irq;
633 if (!chip->core_irq)
Haojian Zhuang5c42e8c2009-12-15 16:01:47 -0500634 goto out;
Haojian Zhuang2afa62e2010-02-08 05:02:00 -0500635
Lee Jones2e578482014-05-08 12:28:37 +0100636 ret = request_threaded_irq(chip->core_irq, NULL, pm860x_irq,
637 flags | IRQF_ONESHOT, "88pm860x", chip);
Haojian Zhuang2afa62e2010-02-08 05:02:00 -0500638 if (ret) {
639 dev_err(chip->dev, "Failed to request IRQ: %d\n", ret);
640 chip->core_irq = 0;
641 }
642
Haojian Zhuang5c42e8c2009-12-15 16:01:47 -0500643 return 0;
644out:
Haojian Zhuang2afa62e2010-02-08 05:02:00 -0500645 chip->core_irq = 0;
Haojian Zhuang5c42e8c2009-12-15 16:01:47 -0500646 return ret;
647}
648
Henrik Kretzschmar872c1b12010-03-26 02:40:13 +0100649static void device_irq_exit(struct pm860x_chip *chip)
Haojian Zhuang5c42e8c2009-12-15 16:01:47 -0500650{
Haojian Zhuang2afa62e2010-02-08 05:02:00 -0500651 if (chip->core_irq)
652 free_irq(chip->core_irq, chip);
Haojian Zhuang5c42e8c2009-12-15 16:01:47 -0500653}
654
Jett.Zhou23de4352012-03-01 11:59:19 +0100655int pm8606_osc_enable(struct pm860x_chip *chip, unsigned short client)
656{
657 int ret = -EIO;
658 struct i2c_client *i2c = (chip->id == CHIP_PM8606) ?
659 chip->client : chip->companion;
660
661 dev_dbg(chip->dev, "%s(B): client=0x%x\n", __func__, client);
662 dev_dbg(chip->dev, "%s(B): vote=0x%x status=%d\n",
663 __func__, chip->osc_vote,
664 chip->osc_status);
665
666 mutex_lock(&chip->osc_lock);
667 /* Update voting status */
668 chip->osc_vote |= client;
669 /* If reference group is off - turn on*/
670 if (chip->osc_status != PM8606_REF_GP_OSC_ON) {
671 chip->osc_status = PM8606_REF_GP_OSC_UNKNOWN;
672 /* Enable Reference group Vsys */
673 if (pm860x_set_bits(i2c, PM8606_VSYS,
674 PM8606_VSYS_EN, PM8606_VSYS_EN))
675 goto out;
676
677 /*Enable Internal Oscillator */
678 if (pm860x_set_bits(i2c, PM8606_MISC,
679 PM8606_MISC_OSC_EN, PM8606_MISC_OSC_EN))
680 goto out;
681 /* Update status (only if writes succeed) */
682 chip->osc_status = PM8606_REF_GP_OSC_ON;
683 }
684 mutex_unlock(&chip->osc_lock);
685
686 dev_dbg(chip->dev, "%s(A): vote=0x%x status=%d ret=%d\n",
687 __func__, chip->osc_vote,
688 chip->osc_status, ret);
689 return 0;
690out:
691 mutex_unlock(&chip->osc_lock);
692 return ret;
693}
Samuel Ortiz2f5f89b2012-03-06 08:06:14 +0100694EXPORT_SYMBOL(pm8606_osc_enable);
Jett.Zhou23de4352012-03-01 11:59:19 +0100695
696int pm8606_osc_disable(struct pm860x_chip *chip, unsigned short client)
697{
698 int ret = -EIO;
699 struct i2c_client *i2c = (chip->id == CHIP_PM8606) ?
700 chip->client : chip->companion;
701
702 dev_dbg(chip->dev, "%s(B): client=0x%x\n", __func__, client);
703 dev_dbg(chip->dev, "%s(B): vote=0x%x status=%d\n",
704 __func__, chip->osc_vote,
705 chip->osc_status);
706
707 mutex_lock(&chip->osc_lock);
708 /*Update voting status */
709 chip->osc_vote &= ~(client);
710 /* If reference group is off and this is the last client to release
711 * - turn off */
712 if ((chip->osc_status != PM8606_REF_GP_OSC_OFF) &&
713 (chip->osc_vote == REF_GP_NO_CLIENTS)) {
714 chip->osc_status = PM8606_REF_GP_OSC_UNKNOWN;
715 /* Disable Reference group Vsys */
716 if (pm860x_set_bits(i2c, PM8606_VSYS, PM8606_VSYS_EN, 0))
717 goto out;
718 /* Disable Internal Oscillator */
719 if (pm860x_set_bits(i2c, PM8606_MISC, PM8606_MISC_OSC_EN, 0))
720 goto out;
721 chip->osc_status = PM8606_REF_GP_OSC_OFF;
722 }
723 mutex_unlock(&chip->osc_lock);
724
725 dev_dbg(chip->dev, "%s(A): vote=0x%x status=%d ret=%d\n",
726 __func__, chip->osc_vote,
727 chip->osc_status, ret);
728 return 0;
729out:
730 mutex_unlock(&chip->osc_lock);
731 return ret;
732}
Samuel Ortiz2f5f89b2012-03-06 08:06:14 +0100733EXPORT_SYMBOL(pm8606_osc_disable);
Jett.Zhou23de4352012-03-01 11:59:19 +0100734
Bill Pembertonf791be42012-11-19 13:23:04 -0500735static void device_osc_init(struct i2c_client *i2c)
Jett.Zhou23de4352012-03-01 11:59:19 +0100736{
737 struct pm860x_chip *chip = i2c_get_clientdata(i2c);
738
739 mutex_init(&chip->osc_lock);
740 /* init portofino reference group voting and status */
741 /* Disable Reference group Vsys */
742 pm860x_set_bits(i2c, PM8606_VSYS, PM8606_VSYS_EN, 0);
743 /* Disable Internal Oscillator */
744 pm860x_set_bits(i2c, PM8606_MISC, PM8606_MISC_OSC_EN, 0);
745
746 chip->osc_vote = REF_GP_NO_CLIENTS;
747 chip->osc_status = PM8606_REF_GP_OSC_OFF;
748}
749
Bill Pembertonf791be42012-11-19 13:23:04 -0500750static void device_bk_init(struct pm860x_chip *chip,
Haojian Zhuangadb70482011-03-07 23:43:09 +0800751 struct pm860x_platform_data *pdata)
752{
Haojian Zhuanga6ccdcd2012-08-08 23:17:26 +0800753 int ret, i;
Haojian Zhuangadb70482011-03-07 23:43:09 +0800754
Haojian Zhuanga6ccdcd2012-08-08 23:17:26 +0800755 if (pdata && pdata->backlight) {
756 if (pdata->num_backlights > ARRAY_SIZE(bk_devs))
757 pdata->num_backlights = ARRAY_SIZE(bk_devs);
758 for (i = 0; i < pdata->num_backlights; i++) {
759 bk_devs[i].platform_data = &pdata->backlight[i];
760 bk_devs[i].pdata_size =
761 sizeof(struct pm860x_backlight_pdata);
Haojian Zhuangadb70482011-03-07 23:43:09 +0800762 }
763 }
Haojian Zhuanga6ccdcd2012-08-08 23:17:26 +0800764 ret = mfd_add_devices(chip->dev, 0, bk_devs,
Mark Brown55692af2012-09-11 15:16:36 +0800765 ARRAY_SIZE(bk_devs), NULL, 0, NULL);
Haojian Zhuanga6ccdcd2012-08-08 23:17:26 +0800766 if (ret < 0)
767 dev_err(chip->dev, "Failed to add backlight subdev\n");
Haojian Zhuangadb70482011-03-07 23:43:09 +0800768}
769
Bill Pembertonf791be42012-11-19 13:23:04 -0500770static void device_led_init(struct pm860x_chip *chip,
Haojian Zhuang3154c342011-03-07 23:43:10 +0800771 struct pm860x_platform_data *pdata)
Haojian Zhuang53dbab72010-01-08 06:01:24 -0500772{
Haojian Zhuang894fc8f2012-08-08 23:17:27 +0800773 int ret, i;
Haojian Zhuanga16122b2009-12-15 16:04:36 -0500774
Haojian Zhuang894fc8f2012-08-08 23:17:27 +0800775 if (pdata && pdata->led) {
776 if (pdata->num_leds > ARRAY_SIZE(led_devs))
777 pdata->num_leds = ARRAY_SIZE(led_devs);
778 for (i = 0; i < pdata->num_leds; i++) {
779 led_devs[i].platform_data = &pdata->led[i];
780 led_devs[i].pdata_size =
781 sizeof(struct pm860x_led_pdata);
Haojian Zhuanga16122b2009-12-15 16:04:36 -0500782 }
783 }
Haojian Zhuang894fc8f2012-08-08 23:17:27 +0800784 ret = mfd_add_devices(chip->dev, 0, led_devs,
Mark Brown55692af2012-09-11 15:16:36 +0800785 ARRAY_SIZE(led_devs), NULL, 0, NULL);
Haojian Zhuang894fc8f2012-08-08 23:17:27 +0800786 if (ret < 0) {
787 dev_err(chip->dev, "Failed to add led subdev\n");
788 return;
789 }
Haojian Zhuang53dbab72010-01-08 06:01:24 -0500790}
791
Bill Pembertonf791be42012-11-19 13:23:04 -0500792static void device_regulator_init(struct pm860x_chip *chip,
Haojian Zhuang22aad002011-03-07 23:43:11 +0800793 struct pm860x_platform_data *pdata)
794{
Haojian Zhuang22aad002011-03-07 23:43:11 +0800795 int ret;
Haojian Zhuang22aad002011-03-07 23:43:11 +0800796
Haojian Zhuanga70abac2012-08-08 23:17:28 +0800797 if (pdata == NULL)
Haojian Zhuang22aad002011-03-07 23:43:11 +0800798 return;
Haojian Zhuanga70abac2012-08-08 23:17:28 +0800799 if (pdata->buck1) {
800 reg_devs[0].platform_data = pdata->buck1;
801 reg_devs[0].pdata_size = sizeof(struct regulator_init_data);
Haojian Zhuang22aad002011-03-07 23:43:11 +0800802 }
Haojian Zhuanga70abac2012-08-08 23:17:28 +0800803 if (pdata->buck2) {
804 reg_devs[1].platform_data = pdata->buck2;
805 reg_devs[1].pdata_size = sizeof(struct regulator_init_data);
806 }
807 if (pdata->buck3) {
808 reg_devs[2].platform_data = pdata->buck3;
809 reg_devs[2].pdata_size = sizeof(struct regulator_init_data);
810 }
811 if (pdata->ldo1) {
812 reg_devs[3].platform_data = pdata->ldo1;
813 reg_devs[3].pdata_size = sizeof(struct regulator_init_data);
814 }
815 if (pdata->ldo2) {
816 reg_devs[4].platform_data = pdata->ldo2;
817 reg_devs[4].pdata_size = sizeof(struct regulator_init_data);
818 }
819 if (pdata->ldo3) {
820 reg_devs[5].platform_data = pdata->ldo3;
821 reg_devs[5].pdata_size = sizeof(struct regulator_init_data);
822 }
823 if (pdata->ldo4) {
824 reg_devs[6].platform_data = pdata->ldo4;
825 reg_devs[6].pdata_size = sizeof(struct regulator_init_data);
826 }
827 if (pdata->ldo5) {
828 reg_devs[7].platform_data = pdata->ldo5;
829 reg_devs[7].pdata_size = sizeof(struct regulator_init_data);
830 }
831 if (pdata->ldo6) {
832 reg_devs[8].platform_data = pdata->ldo6;
833 reg_devs[8].pdata_size = sizeof(struct regulator_init_data);
834 }
835 if (pdata->ldo7) {
836 reg_devs[9].platform_data = pdata->ldo7;
837 reg_devs[9].pdata_size = sizeof(struct regulator_init_data);
838 }
839 if (pdata->ldo8) {
840 reg_devs[10].platform_data = pdata->ldo8;
841 reg_devs[10].pdata_size = sizeof(struct regulator_init_data);
842 }
843 if (pdata->ldo9) {
844 reg_devs[11].platform_data = pdata->ldo9;
845 reg_devs[11].pdata_size = sizeof(struct regulator_init_data);
846 }
847 if (pdata->ldo10) {
848 reg_devs[12].platform_data = pdata->ldo10;
849 reg_devs[12].pdata_size = sizeof(struct regulator_init_data);
850 }
851 if (pdata->ldo12) {
852 reg_devs[13].platform_data = pdata->ldo12;
853 reg_devs[13].pdata_size = sizeof(struct regulator_init_data);
854 }
855 if (pdata->ldo_vibrator) {
856 reg_devs[14].platform_data = pdata->ldo_vibrator;
857 reg_devs[14].pdata_size = sizeof(struct regulator_init_data);
858 }
859 if (pdata->ldo14) {
860 reg_devs[15].platform_data = pdata->ldo14;
861 reg_devs[15].pdata_size = sizeof(struct regulator_init_data);
862 }
863 ret = mfd_add_devices(chip->dev, 0, reg_devs,
Mark Brown55692af2012-09-11 15:16:36 +0800864 ARRAY_SIZE(reg_devs), NULL, 0, NULL);
Haojian Zhuanga70abac2012-08-08 23:17:28 +0800865 if (ret < 0) {
866 dev_err(chip->dev, "Failed to add regulator subdev\n");
867 return;
868 }
Haojian Zhuang22aad002011-03-07 23:43:11 +0800869}
870
Bill Pembertonf791be42012-11-19 13:23:04 -0500871static void device_rtc_init(struct pm860x_chip *chip,
Haojian Zhuang008b3042011-05-06 17:21:20 +0800872 struct pm860x_platform_data *pdata)
873{
874 int ret;
875
Lee Jones2e578482014-05-08 12:28:37 +0100876 if (!pdata)
Haojian Zhuang008b3042011-05-06 17:21:20 +0800877 return;
878
879 rtc_devs[0].platform_data = pdata->rtc;
880 rtc_devs[0].pdata_size = sizeof(struct pm860x_rtc_pdata);
881 rtc_devs[0].num_resources = ARRAY_SIZE(rtc_resources);
882 rtc_devs[0].resources = &rtc_resources[0];
883 ret = mfd_add_devices(chip->dev, 0, &rtc_devs[0],
884 ARRAY_SIZE(rtc_devs), &rtc_resources[0],
Mark Brown55692af2012-09-11 15:16:36 +0800885 chip->irq_base, NULL);
Haojian Zhuang008b3042011-05-06 17:21:20 +0800886 if (ret < 0)
887 dev_err(chip->dev, "Failed to add rtc subdev\n");
888}
889
Bill Pembertonf791be42012-11-19 13:23:04 -0500890static void device_touch_init(struct pm860x_chip *chip,
Haojian Zhuangc9f560b2011-03-07 23:43:12 +0800891 struct pm860x_platform_data *pdata)
892{
893 int ret;
894
Haojian Zhuangf5fb7582011-05-06 17:21:21 +0800895 if (pdata == NULL)
Haojian Zhuangc9f560b2011-03-07 23:43:12 +0800896 return;
897
Haojian Zhuangf5fb7582011-05-06 17:21:21 +0800898 touch_devs[0].platform_data = pdata->touch;
899 touch_devs[0].pdata_size = sizeof(struct pm860x_touch_pdata);
Haojian Zhuangc9f560b2011-03-07 23:43:12 +0800900 touch_devs[0].num_resources = ARRAY_SIZE(touch_resources);
901 touch_devs[0].resources = &touch_resources[0];
902 ret = mfd_add_devices(chip->dev, 0, &touch_devs[0],
903 ARRAY_SIZE(touch_devs), &touch_resources[0],
Mark Brown55692af2012-09-11 15:16:36 +0800904 chip->irq_base, NULL);
Haojian Zhuangc9f560b2011-03-07 23:43:12 +0800905 if (ret < 0)
906 dev_err(chip->dev, "Failed to add touch subdev\n");
907}
908
Bill Pembertonf791be42012-11-19 13:23:04 -0500909static void device_power_init(struct pm860x_chip *chip,
Haojian Zhuangc9f560b2011-03-07 23:43:12 +0800910 struct pm860x_platform_data *pdata)
911{
912 int ret;
913
Haojian Zhuangf5fb7582011-05-06 17:21:21 +0800914 if (pdata == NULL)
Haojian Zhuangc9f560b2011-03-07 23:43:12 +0800915 return;
916
Haojian Zhuangf5fb7582011-05-06 17:21:21 +0800917 power_devs[0].platform_data = pdata->power;
918 power_devs[0].pdata_size = sizeof(struct pm860x_power_pdata);
Haojian Zhuangc9f560b2011-03-07 23:43:12 +0800919 power_devs[0].num_resources = ARRAY_SIZE(battery_resources);
920 power_devs[0].resources = &battery_resources[0],
921 ret = mfd_add_devices(chip->dev, 0, &power_devs[0], 1,
Mark Brown55692af2012-09-11 15:16:36 +0800922 &battery_resources[0], chip->irq_base, NULL);
Haojian Zhuangc9f560b2011-03-07 23:43:12 +0800923 if (ret < 0)
924 dev_err(chip->dev, "Failed to add battery subdev\n");
925
Haojian Zhuangf5fb7582011-05-06 17:21:21 +0800926 power_devs[1].platform_data = pdata->power;
927 power_devs[1].pdata_size = sizeof(struct pm860x_power_pdata);
Haojian Zhuangc9f560b2011-03-07 23:43:12 +0800928 power_devs[1].num_resources = ARRAY_SIZE(charger_resources);
929 power_devs[1].resources = &charger_resources[0],
930 ret = mfd_add_devices(chip->dev, 0, &power_devs[1], 1,
Mark Brown55692af2012-09-11 15:16:36 +0800931 &charger_resources[0], chip->irq_base, NULL);
Haojian Zhuangc9f560b2011-03-07 23:43:12 +0800932 if (ret < 0)
933 dev_err(chip->dev, "Failed to add charger subdev\n");
Jett.Zhou2573f6d2012-07-06 10:59:58 +0800934
935 power_devs[2].platform_data = &preg_init_data;
936 power_devs[2].pdata_size = sizeof(struct regulator_init_data);
Jett.Zhou2573f6d2012-07-06 10:59:58 +0800937 ret = mfd_add_devices(chip->dev, 0, &power_devs[2], 1,
Haojian Zhuangff13e9e2012-09-17 12:19:07 +0800938 NULL, chip->irq_base, NULL);
Jett.Zhou2573f6d2012-07-06 10:59:58 +0800939 if (ret < 0)
940 dev_err(chip->dev, "Failed to add preg subdev\n");
Jett.Zhoua830d282012-07-27 16:27:16 +0800941
942 if (pdata->chg_desc) {
943 pdata->chg_desc->charger_regulators =
944 &chg_desc_regulator_data[0];
945 pdata->chg_desc->num_charger_regulators =
946 ARRAY_SIZE(chg_desc_regulator_data),
947 power_devs[3].platform_data = pdata->chg_desc;
948 power_devs[3].pdata_size = sizeof(*pdata->chg_desc);
949 ret = mfd_add_devices(chip->dev, 0, &power_devs[3], 1,
Anton Vorontsov18766f02012-09-24 19:10:08 -0700950 NULL, chip->irq_base, NULL);
Jett.Zhoua830d282012-07-27 16:27:16 +0800951 if (ret < 0)
952 dev_err(chip->dev, "Failed to add chg-manager subdev\n");
953 }
Haojian Zhuangc9f560b2011-03-07 23:43:12 +0800954}
955
Bill Pembertonf791be42012-11-19 13:23:04 -0500956static void device_onkey_init(struct pm860x_chip *chip,
Haojian Zhuangc9f560b2011-03-07 23:43:12 +0800957 struct pm860x_platform_data *pdata)
958{
959 int ret;
960
961 onkey_devs[0].num_resources = ARRAY_SIZE(onkey_resources);
962 onkey_devs[0].resources = &onkey_resources[0],
963 ret = mfd_add_devices(chip->dev, 0, &onkey_devs[0],
964 ARRAY_SIZE(onkey_devs), &onkey_resources[0],
Mark Brown55692af2012-09-11 15:16:36 +0800965 chip->irq_base, NULL);
Haojian Zhuangc9f560b2011-03-07 23:43:12 +0800966 if (ret < 0)
967 dev_err(chip->dev, "Failed to add onkey subdev\n");
968}
969
Bill Pembertonf791be42012-11-19 13:23:04 -0500970static void device_codec_init(struct pm860x_chip *chip,
Haojian Zhuangc9f560b2011-03-07 23:43:12 +0800971 struct pm860x_platform_data *pdata)
972{
973 int ret;
974
975 codec_devs[0].num_resources = ARRAY_SIZE(codec_resources);
976 codec_devs[0].resources = &codec_resources[0],
977 ret = mfd_add_devices(chip->dev, 0, &codec_devs[0],
Mark Brown55692af2012-09-11 15:16:36 +0800978 ARRAY_SIZE(codec_devs), &codec_resources[0], 0,
979 NULL);
Haojian Zhuangc9f560b2011-03-07 23:43:12 +0800980 if (ret < 0)
981 dev_err(chip->dev, "Failed to add codec subdev\n");
982}
983
Bill Pembertonf791be42012-11-19 13:23:04 -0500984static void device_8607_init(struct pm860x_chip *chip,
Haojian Zhuang5c42e8c2009-12-15 16:01:47 -0500985 struct i2c_client *i2c,
986 struct pm860x_platform_data *pdata)
Haojian Zhuangbbd51b12010-01-06 17:04:18 -0500987{
Haojian Zhuanga16122b2009-12-15 16:04:36 -0500988 int data, ret;
Haojian Zhuangbbd51b12010-01-06 17:04:18 -0500989
Haojian Zhuang53dbab72010-01-08 06:01:24 -0500990 ret = pm860x_reg_read(i2c, PM8607_CHIP_ID);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -0500991 if (ret < 0) {
992 dev_err(chip->dev, "Failed to read CHIP ID: %d\n", ret);
993 goto out;
994 }
Haojian Zhuang38b340522010-09-08 09:44:34 -0400995 switch (ret & PM8607_VERSION_MASK) {
996 case 0x40:
997 case 0x50:
Haojian Zhuangbbd51b12010-01-06 17:04:18 -0500998 dev_info(chip->dev, "Marvell 88PM8607 (ID: %02x) detected\n",
999 ret);
Haojian Zhuang38b340522010-09-08 09:44:34 -04001000 break;
1001 default:
Lee Jones2e578482014-05-08 12:28:37 +01001002 dev_err(chip->dev,
1003 "Failed to detect Marvell 88PM8607. Chip ID: %02x\n",
1004 ret);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -05001005 goto out;
1006 }
Haojian Zhuangbbd51b12010-01-06 17:04:18 -05001007
Haojian Zhuang53dbab72010-01-08 06:01:24 -05001008 ret = pm860x_reg_read(i2c, PM8607_BUCK3);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -05001009 if (ret < 0) {
1010 dev_err(chip->dev, "Failed to read BUCK3 register: %d\n", ret);
1011 goto out;
1012 }
1013 if (ret & PM8607_BUCK3_DOUBLE)
1014 chip->buck3_double = 1;
1015
Haojian Zhuang5c42e8c2009-12-15 16:01:47 -05001016 ret = pm860x_reg_read(i2c, PM8607_B0_MISC1);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -05001017 if (ret < 0) {
1018 dev_err(chip->dev, "Failed to read MISC1 register: %d\n", ret);
1019 goto out;
1020 }
Haojian Zhuangbbd51b12010-01-06 17:04:18 -05001021
Haojian Zhuang5c42e8c2009-12-15 16:01:47 -05001022 if (pdata && (pdata->i2c_port == PI2C_PORT))
1023 data = PM8607_B0_MISC1_PI2C;
1024 else
1025 data = 0;
1026 ret = pm860x_set_bits(i2c, PM8607_B0_MISC1, PM8607_B0_MISC1_PI2C, data);
1027 if (ret < 0) {
1028 dev_err(chip->dev, "Failed to access MISC1:%d\n", ret);
1029 goto out;
1030 }
1031
1032 ret = device_irq_init(chip, pdata);
1033 if (ret < 0)
1034 goto out;
1035
Haojian Zhuangcea438d2011-05-06 17:21:24 +08001036 device_regulator_init(chip, pdata);
1037 device_rtc_init(chip, pdata);
1038 device_onkey_init(chip, pdata);
1039 device_touch_init(chip, pdata);
1040 device_power_init(chip, pdata);
1041 device_codec_init(chip, pdata);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -05001042out:
Haojian Zhuang53dbab72010-01-08 06:01:24 -05001043 return;
Haojian Zhuangbbd51b12010-01-06 17:04:18 -05001044}
1045
Bill Pembertonf791be42012-11-19 13:23:04 -05001046static void device_8606_init(struct pm860x_chip *chip,
Jett.Zhou78258062012-02-28 10:58:42 +08001047 struct i2c_client *i2c,
1048 struct pm860x_platform_data *pdata)
1049{
1050 device_osc_init(i2c);
1051 device_bk_init(chip, pdata);
1052 device_led_init(chip, pdata);
1053}
1054
Bill Pembertonf791be42012-11-19 13:23:04 -05001055static int pm860x_device_init(struct pm860x_chip *chip,
Haojian Zhuangf2f218c2012-09-17 12:19:08 +08001056 struct pm860x_platform_data *pdata)
Haojian Zhuang53dbab72010-01-08 06:01:24 -05001057{
Haojian Zhuang2afa62e2010-02-08 05:02:00 -05001058 chip->core_irq = 0;
Haojian Zhuang5c42e8c2009-12-15 16:01:47 -05001059
Haojian Zhuang53dbab72010-01-08 06:01:24 -05001060 switch (chip->id) {
1061 case CHIP_PM8606:
Jett.Zhou78258062012-02-28 10:58:42 +08001062 device_8606_init(chip, chip->client, pdata);
Haojian Zhuang53dbab72010-01-08 06:01:24 -05001063 break;
1064 case CHIP_PM8607:
1065 device_8607_init(chip, chip->client, pdata);
1066 break;
1067 }
1068
1069 if (chip->companion) {
1070 switch (chip->id) {
1071 case CHIP_PM8607:
Jett.Zhou78258062012-02-28 10:58:42 +08001072 device_8606_init(chip, chip->companion, pdata);
Haojian Zhuang53dbab72010-01-08 06:01:24 -05001073 break;
1074 case CHIP_PM8606:
1075 device_8607_init(chip, chip->companion, pdata);
1076 break;
1077 }
1078 }
Haojian Zhuang5c42e8c2009-12-15 16:01:47 -05001079
Haojian Zhuang53dbab72010-01-08 06:01:24 -05001080 return 0;
1081}
1082
Bill Pemberton4740f732012-11-19 13:26:01 -05001083static void pm860x_device_exit(struct pm860x_chip *chip)
Haojian Zhuangbbd51b12010-01-06 17:04:18 -05001084{
Haojian Zhuang5c42e8c2009-12-15 16:01:47 -05001085 device_irq_exit(chip);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -05001086 mfd_remove_devices(chip->dev);
1087}
1088
Haojian Zhuangf2f218c2012-09-17 12:19:08 +08001089static int verify_addr(struct i2c_client *i2c)
1090{
1091 unsigned short addr_8607[] = {0x30, 0x34};
1092 unsigned short addr_8606[] = {0x10, 0x11};
1093 int size, i;
1094
1095 if (i2c == NULL)
1096 return 0;
1097 size = ARRAY_SIZE(addr_8606);
1098 for (i = 0; i < size; i++) {
1099 if (i2c->addr == *(addr_8606 + i))
1100 return CHIP_PM8606;
1101 }
1102 size = ARRAY_SIZE(addr_8607);
1103 for (i = 0; i < size; i++) {
1104 if (i2c->addr == *(addr_8607 + i))
1105 return CHIP_PM8607;
1106 }
1107 return 0;
1108}
1109
Krzysztof Kozlowski8da90cc2015-01-05 10:01:20 +01001110static const struct regmap_config pm860x_regmap_config = {
Haojian Zhuangf2f218c2012-09-17 12:19:08 +08001111 .reg_bits = 8,
1112 .val_bits = 8,
1113};
1114
Bill Pembertonf791be42012-11-19 13:23:04 -05001115static int pm860x_dt_init(struct device_node *np,
Haojian Zhuang2e57d562012-09-21 18:06:52 +08001116 struct device *dev,
1117 struct pm860x_platform_data *pdata)
1118{
1119 int ret;
1120
1121 if (of_get_property(np, "marvell,88pm860x-irq-read-clr", NULL))
1122 pdata->irq_mode = 1;
1123 ret = of_property_read_u32(np, "marvell,88pm860x-slave-addr",
1124 &pdata->companion_addr);
1125 if (ret) {
Lee Jones2e578482014-05-08 12:28:37 +01001126 dev_err(dev,
1127 "Not found \"marvell,88pm860x-slave-addr\" property\n");
Haojian Zhuang2e57d562012-09-21 18:06:52 +08001128 pdata->companion_addr = 0;
1129 }
1130 return 0;
1131}
1132
Bill Pembertonf791be42012-11-19 13:23:04 -05001133static int pm860x_probe(struct i2c_client *client,
Haojian Zhuangf2f218c2012-09-17 12:19:08 +08001134 const struct i2c_device_id *id)
1135{
Jingoo Han334a41c2013-07-30 17:10:05 +09001136 struct pm860x_platform_data *pdata = dev_get_platdata(&client->dev);
Haojian Zhuang2e57d562012-09-21 18:06:52 +08001137 struct device_node *node = client->dev.of_node;
Haojian Zhuangf2f218c2012-09-17 12:19:08 +08001138 struct pm860x_chip *chip;
1139 int ret;
1140
Haojian Zhuang2e57d562012-09-21 18:06:52 +08001141 if (node && !pdata) {
1142 /* parse DT to get platform data */
1143 pdata = devm_kzalloc(&client->dev,
1144 sizeof(struct pm860x_platform_data),
1145 GFP_KERNEL);
1146 if (!pdata)
1147 return -ENOMEM;
1148 ret = pm860x_dt_init(node, &client->dev, pdata);
1149 if (ret)
Jingoo Han85529572013-02-20 18:27:21 +09001150 return ret;
Haojian Zhuang2e57d562012-09-21 18:06:52 +08001151 } else if (!pdata) {
Haojian Zhuangf2f218c2012-09-17 12:19:08 +08001152 pr_info("No platform data in %s!\n", __func__);
1153 return -EINVAL;
1154 }
1155
Lee Jonesa1ace0a2013-05-23 16:25:03 +01001156 chip = devm_kzalloc(&client->dev,
1157 sizeof(struct pm860x_chip), GFP_KERNEL);
Jingoo Han85529572013-02-20 18:27:21 +09001158 if (chip == NULL)
1159 return -ENOMEM;
Haojian Zhuangf2f218c2012-09-17 12:19:08 +08001160
1161 chip->id = verify_addr(client);
Sachin Kamataaaab422013-06-12 13:55:43 +05301162 chip->regmap = devm_regmap_init_i2c(client, &pm860x_regmap_config);
Haojian Zhuangf2f218c2012-09-17 12:19:08 +08001163 if (IS_ERR(chip->regmap)) {
1164 ret = PTR_ERR(chip->regmap);
1165 dev_err(&client->dev, "Failed to allocate register map: %d\n",
1166 ret);
Haojian Zhuangf2f218c2012-09-17 12:19:08 +08001167 return ret;
1168 }
1169 chip->client = client;
1170 i2c_set_clientdata(client, chip);
1171 chip->dev = &client->dev;
1172 dev_set_drvdata(chip->dev, chip);
1173
1174 /*
1175 * Both client and companion client shares same platform driver.
1176 * Driver distinguishes them by pdata->companion_addr.
1177 * pdata->companion_addr is only assigned if companion chip exists.
1178 * At the same time, the companion_addr shouldn't equal to client
1179 * address.
1180 */
1181 if (pdata->companion_addr && (pdata->companion_addr != client->addr)) {
1182 chip->companion_addr = pdata->companion_addr;
1183 chip->companion = i2c_new_dummy(chip->client->adapter,
1184 chip->companion_addr);
Krzysztof Kozlowski159ce522014-02-11 11:03:29 +01001185 if (!chip->companion) {
1186 dev_err(&client->dev,
1187 "Failed to allocate I2C companion device\n");
1188 return -ENODEV;
1189 }
Haojian Zhuangf2f218c2012-09-17 12:19:08 +08001190 chip->regmap_companion = regmap_init_i2c(chip->companion,
1191 &pm860x_regmap_config);
1192 if (IS_ERR(chip->regmap_companion)) {
1193 ret = PTR_ERR(chip->regmap_companion);
1194 dev_err(&chip->companion->dev,
1195 "Failed to allocate register map: %d\n", ret);
Krzysztof Kozlowskia7ab1c82014-02-11 11:03:30 +01001196 i2c_unregister_device(chip->companion);
Haojian Zhuangf2f218c2012-09-17 12:19:08 +08001197 return ret;
1198 }
1199 i2c_set_clientdata(chip->companion, chip);
1200 }
1201
1202 pm860x_device_init(chip, pdata);
1203 return 0;
1204}
1205
Bill Pemberton4740f732012-11-19 13:26:01 -05001206static int pm860x_remove(struct i2c_client *client)
Haojian Zhuangf2f218c2012-09-17 12:19:08 +08001207{
1208 struct pm860x_chip *chip = i2c_get_clientdata(client);
1209
1210 pm860x_device_exit(chip);
1211 if (chip->companion) {
1212 regmap_exit(chip->regmap_companion);
1213 i2c_unregister_device(chip->companion);
1214 }
Haojian Zhuangf2f218c2012-09-17 12:19:08 +08001215 return 0;
1216}
1217
1218#ifdef CONFIG_PM_SLEEP
1219static int pm860x_suspend(struct device *dev)
1220{
1221 struct i2c_client *client = container_of(dev, struct i2c_client, dev);
1222 struct pm860x_chip *chip = i2c_get_clientdata(client);
1223
1224 if (device_may_wakeup(dev) && chip->wakeup_flag)
1225 enable_irq_wake(chip->core_irq);
1226 return 0;
1227}
1228
1229static int pm860x_resume(struct device *dev)
1230{
1231 struct i2c_client *client = container_of(dev, struct i2c_client, dev);
1232 struct pm860x_chip *chip = i2c_get_clientdata(client);
1233
1234 if (device_may_wakeup(dev) && chip->wakeup_flag)
1235 disable_irq_wake(chip->core_irq);
1236 return 0;
1237}
1238#endif
1239
1240static SIMPLE_DEV_PM_OPS(pm860x_pm_ops, pm860x_suspend, pm860x_resume);
1241
Haojian Zhuang2e57d562012-09-21 18:06:52 +08001242static const struct i2c_device_id pm860x_id_table[] = {
1243 { "88PM860x", 0 },
1244 {}
1245};
1246MODULE_DEVICE_TABLE(i2c, pm860x_id_table);
1247
1248static const struct of_device_id pm860x_dt_ids[] = {
1249 { .compatible = "marvell,88pm860x", },
1250 {},
1251};
1252MODULE_DEVICE_TABLE(of, pm860x_dt_ids);
1253
Haojian Zhuangf2f218c2012-09-17 12:19:08 +08001254static struct i2c_driver pm860x_driver = {
1255 .driver = {
1256 .name = "88PM860x",
Haojian Zhuangf2f218c2012-09-17 12:19:08 +08001257 .pm = &pm860x_pm_ops,
Sachin Kamatcd2a5532013-10-15 09:18:48 +05301258 .of_match_table = pm860x_dt_ids,
Haojian Zhuangf2f218c2012-09-17 12:19:08 +08001259 },
1260 .probe = pm860x_probe,
Bill Pemberton84449212012-11-19 13:20:24 -05001261 .remove = pm860x_remove,
Haojian Zhuangf2f218c2012-09-17 12:19:08 +08001262 .id_table = pm860x_id_table,
1263};
1264
1265static int __init pm860x_i2c_init(void)
1266{
1267 int ret;
1268 ret = i2c_add_driver(&pm860x_driver);
1269 if (ret != 0)
1270 pr_err("Failed to register 88PM860x I2C driver: %d\n", ret);
1271 return ret;
1272}
1273subsys_initcall(pm860x_i2c_init);
1274
1275static void __exit pm860x_i2c_exit(void)
1276{
1277 i2c_del_driver(&pm860x_driver);
1278}
1279module_exit(pm860x_i2c_exit);
1280
Haojian Zhuang53dbab72010-01-08 06:01:24 -05001281MODULE_DESCRIPTION("PMIC Driver for Marvell 88PM860x");
Haojian Zhuangbbd51b12010-01-06 17:04:18 -05001282MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
1283MODULE_LICENSE("GPL");