blob: 814b59ca9d621f321c755168421a966984ba5054 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* Copyright (c) 2009-2011, Code Aurora Forum. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
13/*
14 * Qualcomm PMIC8058 driver
15 *
16 */
Anirudh Ghayalc2019332011-11-12 06:29:10 +053017#include <linux/kernel.h>
18#include <linux/platform_device.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070019#include <linux/slab.h>
Anirudh Ghayalc2019332011-11-12 06:29:10 +053020#include <linux/irq.h>
Anirudh Ghayalf1f1e142011-10-10 17:47:45 +053021#include <linux/msm_ssbi.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070022#include <linux/mfd/core.h>
23#include <linux/mfd/pmic8058.h>
Anirudh Ghayalc2019332011-11-12 06:29:10 +053024#include <linux/mfd/pm8xxx/core.h>
25#include <linux/msm_adc.h>
26
27#define REG_MPP_BASE 0x50
Anirudh Ghayalca42c7de2011-11-21 10:42:07 +053028#define REG_IRQ_BASE 0x1BB
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070029
30/* PMIC8058 Revision */
Anirudh Ghayalc2019332011-11-12 06:29:10 +053031#define PM8058_REG_REV 0x002 /* PMIC4 revision */
32#define PM8058_VERSION_MASK 0xF0
33#define PM8058_REVISION_MASK 0x0F
34#define PM8058_VERSION_VALUE 0xE0
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070035
Anirudh Ghayalc2019332011-11-12 06:29:10 +053036/* PMIC 8058 Battery Alarm SSBI registers */
37#define REG_BATT_ALARM_THRESH 0x023
38#define REG_BATT_ALARM_CTRL1 0x024
39#define REG_BATT_ALARM_CTRL2 0x0AA
40#define REG_BATT_ALARM_PWM_CTRL 0x0A3
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070041
Anirudh Ghayalc2019332011-11-12 06:29:10 +053042#define REG_TEMP_ALRM_CTRL 0x1B
43#define REG_TEMP_ALRM_PWM 0x9B
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070044
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070045/* PON CNTL 4 register */
46#define SSBI_REG_ADDR_PON_CNTL_4 0x98
47#define PM8058_PON_RESET_EN_MASK 0x01
48
49/* PON CNTL 5 register */
50#define SSBI_REG_ADDR_PON_CNTL_5 0x7B
51#define PM8058_HARD_RESET_EN_MASK 0x08
52
Willie Ruan6a3c9142011-07-14 16:52:41 -070053/* GP_TEST1 register */
54#define SSBI_REG_ADDR_GP_TEST_1 0x07A
55
Anirudh Ghayalc2019332011-11-12 06:29:10 +053056#define PM8058_RTC_BASE 0x1E8
57#define PM8058_OTHC_CNTR_BASE0 0xA0
58#define PM8058_OTHC_CNTR_BASE1 0x134
59#define PM8058_OTHC_CNTR_BASE2 0x137
60
61#define SINGLE_IRQ_RESOURCE(_name, _irq) \
62{ \
63 .name = _name, \
64 .start = _irq, \
65 .end = _irq, \
66 .flags = IORESOURCE_IRQ, \
67}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070068
69struct pm8058_chip {
70 struct pm8058_platform_data pdata;
Anirudh Ghayalf1f1e142011-10-10 17:47:45 +053071 struct device *dev;
Anirudh Ghayalc2019332011-11-12 06:29:10 +053072 struct pm_irq_chip *irq_chip;
73 struct mfd_cell *mfd_regulators, *mfd_xo_buffers;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070074
Anirudh Ghayalc2019332011-11-12 06:29:10 +053075 u8 revision;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070076};
77
Anirudh Ghayalc2019332011-11-12 06:29:10 +053078static int pm8058_readb(const struct device *dev, u16 addr, u8 *val)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070079{
Anirudh Ghayalc2019332011-11-12 06:29:10 +053080 const struct pm8xxx_drvdata *pm8058_drvdata = dev_get_drvdata(dev);
81 const struct pm8058_chip *pmic = pm8058_drvdata->pm_chip_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070082
Anirudh Ghayalc2019332011-11-12 06:29:10 +053083 return msm_ssbi_read(pmic->dev->parent, addr, val, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070084}
85
Anirudh Ghayalc2019332011-11-12 06:29:10 +053086static int pm8058_writeb(const struct device *dev, u16 addr, u8 val)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070087{
Anirudh Ghayalc2019332011-11-12 06:29:10 +053088 const struct pm8xxx_drvdata *pm8058_drvdata = dev_get_drvdata(dev);
89 const struct pm8058_chip *pmic = pm8058_drvdata->pm_chip_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070090
Anirudh Ghayalc2019332011-11-12 06:29:10 +053091 return msm_ssbi_write(pmic->dev->parent, addr, &val, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070092}
93
Anirudh Ghayalc2019332011-11-12 06:29:10 +053094static int pm8058_read_buf(const struct device *dev, u16 addr, u8 *buf,
95 int cnt)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070096{
Anirudh Ghayalc2019332011-11-12 06:29:10 +053097 const struct pm8xxx_drvdata *pm8058_drvdata = dev_get_drvdata(dev);
98 const struct pm8058_chip *pmic = pm8058_drvdata->pm_chip_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070099
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530100 return msm_ssbi_read(pmic->dev->parent, addr, buf, cnt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700101}
102
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530103static int pm8058_write_buf(const struct device *dev, u16 addr, u8 *buf,
104 int cnt)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700105{
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530106 const struct pm8xxx_drvdata *pm8058_drvdata = dev_get_drvdata(dev);
107 const struct pm8058_chip *pmic = pm8058_drvdata->pm_chip_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700108
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530109 return msm_ssbi_write(pmic->dev->parent, addr, buf, cnt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700110}
111
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530112static int pm8058_read_irq_stat(const struct device *dev, int irq)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700113{
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530114 const struct pm8xxx_drvdata *pm8058_drvdata = dev_get_drvdata(dev);
115 const struct pm8058_chip *pmic = pm8058_drvdata->pm_chip_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700116
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530117 return pm8xxx_get_irq_stat(pmic->irq_chip, irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700118
119 return 0;
120}
121
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530122static enum pm8xxx_version pm8058_get_version(const struct device *dev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700123{
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530124 const struct pm8xxx_drvdata *pm8058_drvdata = dev_get_drvdata(dev);
125 const struct pm8058_chip *pmic = pm8058_drvdata->pm_chip_data;
126 enum pm8xxx_version version = -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700127
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530128 if ((pmic->revision & PM8058_VERSION_MASK) == PM8058_VERSION_VALUE)
129 version = PM8XXX_VERSION_8058;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700130
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530131 return version;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700132}
133
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530134static int pm8058_get_revision(const struct device *dev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700135{
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530136 const struct pm8xxx_drvdata *pm8058_drvdata = dev_get_drvdata(dev);
137 const struct pm8058_chip *pmic = pm8058_drvdata->pm_chip_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700138
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530139 return pmic->revision & PM8058_REVISION_MASK;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700140}
141
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530142static struct pm8xxx_drvdata pm8058_drvdata = {
143 .pmic_readb = pm8058_readb,
144 .pmic_writeb = pm8058_writeb,
145 .pmic_read_buf = pm8058_read_buf,
146 .pmic_write_buf = pm8058_write_buf,
147 .pmic_read_irq_stat = pm8058_read_irq_stat,
148 .pmic_get_version = pm8058_get_version,
149 .pmic_get_revision = pm8058_get_revision,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700150};
151
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530152static const struct resource pm8058_charger_resources[] __devinitconst = {
153 SINGLE_IRQ_RESOURCE("CHGVAL", PM8058_CHGVAL_IRQ),
154 SINGLE_IRQ_RESOURCE("CHGINVAL", PM8058_CHGINVAL_IRQ),
155 SINGLE_IRQ_RESOURCE("CHGILIM", PM8058_CHGILIM_IRQ),
156 SINGLE_IRQ_RESOURCE("VCP", PM8058_VCP_IRQ),
157 SINGLE_IRQ_RESOURCE("ATC_DONE", PM8058_ATC_DONE_IRQ),
158 SINGLE_IRQ_RESOURCE("ATCFAIL", PM8058_ATCFAIL_IRQ),
159 SINGLE_IRQ_RESOURCE("AUTO_CHGDONE", PM8058_AUTO_CHGDONE_IRQ),
160 SINGLE_IRQ_RESOURCE("AUTO_CHGFAIL", PM8058_AUTO_CHGFAIL_IRQ),
161 SINGLE_IRQ_RESOURCE("CHGSTATE", PM8058_CHGSTATE_IRQ),
162 SINGLE_IRQ_RESOURCE("FASTCHG", PM8058_FASTCHG_IRQ),
163 SINGLE_IRQ_RESOURCE("CHG_END", PM8058_CHG_END_IRQ),
164 SINGLE_IRQ_RESOURCE("BATTTEMP", PM8058_BATTTEMP_IRQ),
165 SINGLE_IRQ_RESOURCE("CHGHOT", PM8058_CHGHOT_IRQ),
166 SINGLE_IRQ_RESOURCE("CHGTLIMIT", PM8058_CHGTLIMIT_IRQ),
167 SINGLE_IRQ_RESOURCE("CHG_GONE", PM8058_CHG_GONE_IRQ),
168 SINGLE_IRQ_RESOURCE("VCPMAJOR", PM8058_VCPMAJOR_IRQ),
169 SINGLE_IRQ_RESOURCE("VBATDET", PM8058_VBATDET_IRQ),
170 SINGLE_IRQ_RESOURCE("BATFET", PM8058_BATFET_IRQ),
171 SINGLE_IRQ_RESOURCE("BATT_REPLACE", PM8058_BATT_REPLACE_IRQ),
172 SINGLE_IRQ_RESOURCE("BATTCONNECT", PM8058_BATTCONNECT_IRQ),
173 SINGLE_IRQ_RESOURCE("VBATDET_LOW", PM8058_VBATDET_LOW_IRQ),
Abhijeet Dharmapurikara3c0d942011-07-25 17:53:45 -0700174};
175
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530176static struct mfd_cell pm8058_charger_cell __devinitdata = {
177 .name = "pm8058-charger",
178 .id = -1,
179 .resources = pm8058_charger_resources,
180 .num_resources = ARRAY_SIZE(pm8058_charger_resources),
181};
182
183static const struct resource misc_cell_resources[] __devinitconst = {
184 SINGLE_IRQ_RESOURCE("pm8xxx_osc_halt_irq", PM8058_OSCHALT_IRQ),
185};
186
187static struct mfd_cell misc_cell __devinitdata = {
188 .name = PM8XXX_MISC_DEV_NAME,
189 .id = -1,
190 .resources = misc_cell_resources,
191 .num_resources = ARRAY_SIZE(misc_cell_resources),
192};
193
194static struct mfd_cell pm8058_pwm_cell __devinitdata = {
195 .name = "pm8058-pwm",
196 .id = -1,
197};
198
199static struct resource xoadc_resources[] = {
200 SINGLE_IRQ_RESOURCE(NULL, PM8058_ADC_IRQ),
201};
202
203static struct mfd_cell xoadc_cell __devinitdata = {
204 .name = "pm8058-xoadc",
205 .id = -1,
206 .resources = xoadc_resources,
207 .num_resources = ARRAY_SIZE(xoadc_resources),
208};
209
210static const struct resource thermal_alarm_cell_resources[] __devinitconst = {
211 SINGLE_IRQ_RESOURCE("pm8058_tempstat_irq", PM8058_TEMPSTAT_IRQ),
212 SINGLE_IRQ_RESOURCE("pm8058_overtemp_irq", PM8058_OVERTEMP_IRQ),
213};
214
215static struct pm8xxx_tm_core_data thermal_alarm_cdata = {
216 .adc_channel = CHANNEL_ADC_DIE_TEMP,
217 .adc_type = PM8XXX_TM_ADC_PM8058_ADC,
218 .reg_addr_temp_alarm_ctrl = REG_TEMP_ALRM_CTRL,
219 .reg_addr_temp_alarm_pwm = REG_TEMP_ALRM_PWM,
220 .tm_name = "pm8058_tz",
221 .irq_name_temp_stat = "pm8058_tempstat_irq",
222 .irq_name_over_temp = "pm8058_overtemp_irq",
223};
224
225static struct mfd_cell thermal_alarm_cell __devinitdata = {
226 .name = PM8XXX_TM_DEV_NAME,
227 .id = -1,
228 .resources = thermal_alarm_cell_resources,
229 .num_resources = ARRAY_SIZE(thermal_alarm_cell_resources),
230 .platform_data = &thermal_alarm_cdata,
231 .pdata_size = sizeof(struct pm8xxx_tm_core_data),
232};
233
234static struct mfd_cell debugfs_cell __devinitdata = {
235 .name = "pm8xxx-debug",
236 .id = -1,
237 .platform_data = "pm8058-dbg",
238 .pdata_size = sizeof("pm8058-dbg"),
239};
240
241static const struct resource othc0_cell_resources[] __devinitconst = {
242 {
243 .name = "othc_base",
244 .start = PM8058_OTHC_CNTR_BASE0,
245 .end = PM8058_OTHC_CNTR_BASE0,
246 .flags = IORESOURCE_IO,
247 },
248};
249
250static const struct resource othc1_cell_resources[] __devinitconst = {
251 SINGLE_IRQ_RESOURCE(NULL, PM8058_SW_1_IRQ),
252 SINGLE_IRQ_RESOURCE(NULL, PM8058_IR_1_IRQ),
253 {
254 .name = "othc_base",
255 .start = PM8058_OTHC_CNTR_BASE1,
256 .end = PM8058_OTHC_CNTR_BASE1,
257 .flags = IORESOURCE_IO,
258 },
259};
260
261static const struct resource othc2_cell_resources[] __devinitconst = {
262 {
263 .name = "othc_base",
264 .start = PM8058_OTHC_CNTR_BASE2,
265 .end = PM8058_OTHC_CNTR_BASE2,
266 .flags = IORESOURCE_IO,
267 },
268};
269
270static const struct resource batt_alarm_cell_resources[] __devinitconst = {
271 SINGLE_IRQ_RESOURCE("pm8058_batt_alarm_irq", PM8058_BATT_ALARM_IRQ),
272};
273
274static struct mfd_cell leds_cell __devinitdata = {
275 .name = "pm8058-led",
276 .id = -1,
277};
278
279static struct mfd_cell othc0_cell __devinitdata = {
280 .name = "pm8058-othc",
281 .id = 0,
282 .resources = othc0_cell_resources,
283 .num_resources = ARRAY_SIZE(othc0_cell_resources),
284};
285
286static struct mfd_cell othc1_cell __devinitdata = {
287 .name = "pm8058-othc",
288 .id = 1,
289 .resources = othc1_cell_resources,
290 .num_resources = ARRAY_SIZE(othc1_cell_resources),
291};
292
293static struct mfd_cell othc2_cell __devinitdata = {
294 .name = "pm8058-othc",
295 .id = 2,
296 .resources = othc2_cell_resources,
297 .num_resources = ARRAY_SIZE(othc2_cell_resources),
298};
299
300static struct pm8xxx_batt_alarm_core_data batt_alarm_cdata = {
301 .irq_name = "pm8058_batt_alarm_irq",
302 .reg_addr_threshold = REG_BATT_ALARM_THRESH,
303 .reg_addr_ctrl1 = REG_BATT_ALARM_CTRL1,
304 .reg_addr_ctrl2 = REG_BATT_ALARM_CTRL2,
305 .reg_addr_pwm_ctrl = REG_BATT_ALARM_PWM_CTRL,
306};
307
308static struct mfd_cell batt_alarm_cell __devinitdata = {
309 .name = PM8XXX_BATT_ALARM_DEV_NAME,
310 .id = -1,
311 .resources = batt_alarm_cell_resources,
312 .num_resources = ARRAY_SIZE(batt_alarm_cell_resources),
313 .platform_data = &batt_alarm_cdata,
314 .pdata_size = sizeof(struct pm8xxx_batt_alarm_core_data),
315};
316
317static struct mfd_cell upl_cell __devinitdata = {
318 .name = PM8XXX_UPL_DEV_NAME,
319 .id = -1,
320};
321
322static struct mfd_cell nfc_cell __devinitdata = {
323 .name = PM8XXX_NFC_DEV_NAME,
324 .id = -1,
325};
326
327static const struct resource rtc_cell_resources[] __devinitconst = {
328 [0] = SINGLE_IRQ_RESOURCE(NULL, PM8058_RTC_ALARM_IRQ),
329 [1] = {
330 .name = "pmic_rtc_base",
331 .start = PM8058_RTC_BASE,
332 .end = PM8058_RTC_BASE,
333 .flags = IORESOURCE_IO,
334 },
335};
336
337static struct mfd_cell rtc_cell __devinitdata = {
338 .name = PM8XXX_RTC_DEV_NAME,
339 .id = -1,
340 .resources = rtc_cell_resources,
341 .num_resources = ARRAY_SIZE(rtc_cell_resources),
342};
343
344static const struct resource resources_pwrkey[] __devinitconst = {
345 SINGLE_IRQ_RESOURCE(NULL, PM8058_PWRKEY_REL_IRQ),
346 SINGLE_IRQ_RESOURCE(NULL, PM8058_PWRKEY_PRESS_IRQ),
347};
348
349static struct mfd_cell vibrator_cell __devinitdata = {
350 .name = PM8XXX_VIBRATOR_DEV_NAME,
351 .id = -1,
352};
353
354static struct mfd_cell pwrkey_cell __devinitdata = {
355 .name = PM8XXX_PWRKEY_DEV_NAME,
356 .id = -1,
357 .num_resources = ARRAY_SIZE(resources_pwrkey),
358 .resources = resources_pwrkey,
359};
360
361static const struct resource resources_keypad[] = {
362 SINGLE_IRQ_RESOURCE(NULL, PM8058_KEYPAD_IRQ),
363 SINGLE_IRQ_RESOURCE(NULL, PM8058_KEYSTUCK_IRQ),
364};
365
366static struct mfd_cell keypad_cell __devinitdata = {
367 .name = PM8XXX_KEYPAD_DEV_NAME,
368 .id = -1,
369 .num_resources = ARRAY_SIZE(resources_keypad),
370 .resources = resources_keypad,
371};
372
373static const struct resource mpp_cell_resources[] __devinitconst = {
374 {
375 .start = PM8058_IRQ_BLOCK_BIT(PM8058_MPP_BLOCK_START, 0),
376 .end = PM8058_IRQ_BLOCK_BIT(PM8058_MPP_BLOCK_START, 0)
377 + PM8058_MPPS - 1,
378 .flags = IORESOURCE_IRQ,
379 },
380};
381
382static struct mfd_cell mpp_cell __devinitdata = {
383 .name = PM8XXX_MPP_DEV_NAME,
384 .id = 0,
385 .resources = mpp_cell_resources,
386 .num_resources = ARRAY_SIZE(mpp_cell_resources),
387};
388
389static const struct resource gpio_cell_resources[] __devinitconst = {
390 [0] = {
391 .start = PM8058_IRQ_BLOCK_BIT(PM8058_GPIO_BLOCK_START, 0),
392 .end = PM8058_IRQ_BLOCK_BIT(PM8058_GPIO_BLOCK_START, 0)
393 + PM8058_GPIOS - 1,
394 .flags = IORESOURCE_IRQ,
395 },
396};
397
398static struct mfd_cell gpio_cell __devinitdata = {
399 .name = PM8XXX_GPIO_DEV_NAME,
400 .id = -1,
401 .resources = gpio_cell_resources,
402 .num_resources = ARRAY_SIZE(gpio_cell_resources),
403};
404
405static int __devinit
406pm8058_add_subdevices(const struct pm8058_platform_data *pdata,
407 struct pm8058_chip *pmic)
408{
409 int rc = 0, irq_base = 0, i;
410 struct pm_irq_chip *irq_chip;
411 static struct mfd_cell *mfd_regulators, *mfd_xo_buffers;
412
413 if (pdata->irq_pdata) {
414 pdata->irq_pdata->irq_cdata.nirqs = PM8058_NR_IRQS;
Anirudh Ghayalca42c7de2011-11-21 10:42:07 +0530415 pdata->irq_pdata->irq_cdata.base_addr = REG_IRQ_BASE;
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530416 irq_base = pdata->irq_pdata->irq_base;
417 irq_chip = pm8xxx_irq_init(pmic->dev, pdata->irq_pdata);
418
419 if (IS_ERR(irq_chip)) {
420 pr_err("Failed to init interrupts ret=%ld\n",
421 PTR_ERR(irq_chip));
422 return PTR_ERR(irq_chip);
423 }
424 pmic->irq_chip = irq_chip;
425 }
426
427 if (pdata->gpio_pdata) {
428 pdata->gpio_pdata->gpio_cdata.ngpios = PM8058_GPIOS;
429 gpio_cell.platform_data = pdata->gpio_pdata;
430 gpio_cell.pdata_size = sizeof(struct pm8xxx_gpio_platform_data);
431 rc = mfd_add_devices(pmic->dev, 0, &gpio_cell, 1,
432 NULL, irq_base);
433 if (rc) {
434 pr_err("Failed to add gpio subdevice ret=%d\n", rc);
435 goto bail;
436 }
437 }
438
439 if (pdata->mpp_pdata) {
440 pdata->mpp_pdata->core_data.nmpps = PM8058_MPPS;
441 pdata->mpp_pdata->core_data.base_addr = REG_MPP_BASE;
442 mpp_cell.platform_data = pdata->mpp_pdata;
443 mpp_cell.pdata_size = sizeof(struct pm8xxx_mpp_platform_data);
444 rc = mfd_add_devices(pmic->dev, 0, &mpp_cell, 1, NULL,
445 irq_base);
446 if (rc) {
447 pr_err("Failed to add mpp subdevice ret=%d\n", rc);
448 goto bail;
449 }
450 }
451
452 if (pdata->num_regulators > 0 && pdata->regulator_pdatas) {
453 mfd_regulators = kzalloc(sizeof(struct mfd_cell)
454 * (pdata->num_regulators), GFP_KERNEL);
455 if (!mfd_regulators) {
456 pr_err("Cannot allocate %d bytes for pm8058 regulator "
457 "mfd cells\n", sizeof(struct mfd_cell)
458 * (pdata->num_regulators));
459 rc = -ENOMEM;
460 goto bail;
461 }
462 for (i = 0; i < pdata->num_regulators; i++) {
463 mfd_regulators[i].name = "pm8058-regulator";
464 mfd_regulators[i].id = pdata->regulator_pdatas[i].id;
465 mfd_regulators[i].platform_data =
466 &(pdata->regulator_pdatas[i]);
467 mfd_regulators[i].pdata_size =
468 sizeof(struct pm8058_vreg_pdata);
469 }
470 rc = mfd_add_devices(pmic->dev, 0, mfd_regulators,
471 pdata->num_regulators, NULL, irq_base);
472 if (rc) {
473 pr_err("Failed to add regulator subdevices ret=%d\n",
474 rc);
475 kfree(mfd_regulators);
476 goto bail;
477 }
478 pmic->mfd_regulators = mfd_regulators;
479 }
480
481 if (pdata->num_xo_buffers > 0 && pdata->xo_buffer_pdata) {
482 mfd_xo_buffers = kzalloc(sizeof(struct mfd_cell)
483 * (pdata->num_xo_buffers), GFP_KERNEL);
484 if (!mfd_xo_buffers) {
485 pr_err("Cannot allocate %d bytes for pm8058 XO buffer "
486 "mfd cells\n", sizeof(struct mfd_cell)
487 * (pdata->num_xo_buffers));
488 rc = -ENOMEM;
489 goto bail;
490 }
491 for (i = 0; i < pdata->num_xo_buffers; i++) {
492 mfd_xo_buffers[i].name = PM8058_XO_BUFFER_DEV_NAME;
493 mfd_xo_buffers[i].id = pdata->xo_buffer_pdata[i].id;
494 mfd_xo_buffers[i].platform_data =
495 &(pdata->xo_buffer_pdata[i]);
496 mfd_xo_buffers[i].pdata_size =
497 sizeof(struct pm8058_xo_pdata);
498 }
499 rc = mfd_add_devices(pmic->dev, 0, mfd_xo_buffers,
500 pdata->num_xo_buffers, NULL, irq_base);
501 if (rc) {
502 pr_err("Failed to add XO buffer subdevices ret=%d\n",
503 rc);
504 kfree(mfd_xo_buffers);
505 goto bail;
506 }
507 pmic->mfd_xo_buffers = mfd_xo_buffers;
508 }
509
510 if (pdata->keypad_pdata) {
511 keypad_cell.platform_data = pdata->keypad_pdata;
512 keypad_cell.pdata_size =
513 sizeof(struct pm8xxx_keypad_platform_data);
514 rc = mfd_add_devices(pmic->dev, 0, &keypad_cell, 1, NULL,
515 irq_base);
516 if (rc) {
517 pr_err("Failed to add keypad subdevice ret=%d\n", rc);
518 goto bail;
519 }
520 }
521
522 if (pdata->rtc_pdata) {
523 rtc_cell.platform_data = pdata->rtc_pdata;
524 rtc_cell.pdata_size = sizeof(struct pm8xxx_rtc_platform_data);
525 rc = mfd_add_devices(pmic->dev, 0, &rtc_cell, 1, NULL,
526 irq_base);
527 if (rc) {
528 pr_err("Failed to add rtc subdevice ret=%d\n", rc);
529 goto bail;
530 }
531 }
532
533 if (pdata->pwrkey_pdata) {
534 pwrkey_cell.platform_data = pdata->pwrkey_pdata;
535 pwrkey_cell.pdata_size =
536 sizeof(struct pm8xxx_pwrkey_platform_data);
537 rc = mfd_add_devices(pmic->dev, 0, &pwrkey_cell, 1, NULL,
538 irq_base);
539 if (rc) {
540 pr_err("Failed to add pwrkey subdevice ret=%d\n", rc);
541 goto bail;
542 }
543 }
544
545 if (pdata->vibrator_pdata) {
546 vibrator_cell.platform_data = pdata->vibrator_pdata;
547 vibrator_cell.pdata_size =
548 sizeof(struct pm8xxx_vibrator_platform_data);
549 rc = mfd_add_devices(pmic->dev, 0, &vibrator_cell, 1, NULL,
550 irq_base);
551 if (rc) {
552 pr_err("Failed to add vibrator subdevice ret=%d\n",
553 rc);
554 goto bail;
555 }
556 }
557
558 if (pdata->leds_pdata) {
559 leds_cell.platform_data = pdata->leds_pdata;
560 leds_cell.pdata_size =
561 sizeof(struct pmic8058_leds_platform_data);
562 rc = mfd_add_devices(pmic->dev, 0, &leds_cell, 1, NULL,
563 irq_base);
564 if (rc) {
565 pr_err("Failed to add leds subdevice ret=%d\n", rc);
566 goto bail;
567 }
568 }
569
570 if (pdata->xoadc_pdata) {
571 xoadc_cell.platform_data = pdata->xoadc_pdata;
572 xoadc_cell.pdata_size =
573 sizeof(struct xoadc_platform_data);
574 rc = mfd_add_devices(pmic->dev, 0, &xoadc_cell, 1, NULL,
575 irq_base);
576 if (rc) {
577 pr_err("Failed to add leds subdevice ret=%d\n", rc);
578 goto bail;
579 }
580 }
581
582 if (pdata->othc0_pdata) {
583 othc0_cell.platform_data = pdata->othc0_pdata;
584 othc0_cell.pdata_size =
585 sizeof(struct pmic8058_othc_config_pdata);
586 rc = mfd_add_devices(pmic->dev, 0, &othc0_cell, 1, NULL, 0);
587 if (rc) {
588 pr_err("Failed to add othc0 subdevice ret=%d\n", rc);
589 goto bail;
590 }
591 }
592
593 if (pdata->othc1_pdata) {
594 othc1_cell.platform_data = pdata->othc1_pdata;
595 othc1_cell.pdata_size =
596 sizeof(struct pmic8058_othc_config_pdata);
597 rc = mfd_add_devices(pmic->dev, 0, &othc1_cell, 1, NULL,
598 irq_base);
599 if (rc) {
600 pr_err("Failed to add othc1 subdevice ret=%d\n", rc);
601 goto bail;
602 }
603 }
604
605 if (pdata->othc2_pdata) {
606 othc2_cell.platform_data = pdata->othc2_pdata;
607 othc2_cell.pdata_size =
608 sizeof(struct pmic8058_othc_config_pdata);
609 rc = mfd_add_devices(pmic->dev, 0, &othc2_cell, 1, NULL, 0);
610 if (rc) {
611 pr_err("Failed to add othc2 subdevice ret=%d\n", rc);
612 goto bail;
613 }
614 }
615
616 if (pdata->pwm_pdata) {
617 pm8058_pwm_cell.platform_data = pdata->pwm_pdata;
618 pm8058_pwm_cell.pdata_size = sizeof(struct pm8058_pwm_pdata);
619 rc = mfd_add_devices(pmic->dev, 0, &pm8058_pwm_cell, 1, NULL,
620 irq_base);
621 if (rc) {
622 pr_err("Failed to add pwm subdevice ret=%d\n", rc);
623 goto bail;
624 }
625 }
626
627 if (pdata->misc_pdata) {
628 misc_cell.platform_data = pdata->misc_pdata;
629 misc_cell.pdata_size = sizeof(struct pm8xxx_misc_platform_data);
630 rc = mfd_add_devices(pmic->dev, 0, &misc_cell, 1, NULL,
631 irq_base);
632 if (rc) {
633 pr_err("Failed to add misc subdevice ret=%d\n", rc);
634 goto bail;
635 }
636 }
637
638 rc = mfd_add_devices(pmic->dev, 0, &thermal_alarm_cell, 1, NULL,
639 irq_base);
640 if (rc) {
641 pr_err("Failed to add thermal alarm subdevice ret=%d\n",
642 rc);
643 goto bail;
644 }
645
646 rc = mfd_add_devices(pmic->dev, 0, &batt_alarm_cell, 1, NULL,
647 irq_base);
648 if (rc) {
649 pr_err("Failed to add battery alarm subdevice ret=%d\n",
650 rc);
651 goto bail;
652 }
653
654 rc = mfd_add_devices(pmic->dev, 0, &upl_cell, 1, NULL, 0);
655 if (rc) {
656 pr_err("Failed to add upl subdevice ret=%d\n", rc);
657 goto bail;
658 }
659
660 rc = mfd_add_devices(pmic->dev, 0, &nfc_cell, 1, NULL, 0);
661 if (rc) {
662 pr_err("Failed to add upl subdevice ret=%d\n", rc);
663 goto bail;
664 }
665
666 if (pdata->charger_pdata) {
667 pm8058_charger_cell.platform_data = pdata->charger_pdata;
668 pm8058_charger_cell.pdata_size = sizeof(struct
669 pmic8058_charger_data);
670 rc = mfd_add_devices(pmic->dev, 0, &pm8058_charger_cell,
671 1, NULL, irq_base);
672 if (rc) {
673 pr_err("Failed to add charger subdevice ret=%d\n", rc);
674 goto bail;
675 }
676 }
677
678 rc = mfd_add_devices(pmic->dev, 0, &debugfs_cell, 1, NULL, irq_base);
679 if (rc) {
680 pr_err("Failed to add debugfs subdevice ret=%d\n", rc);
681 goto bail;
682 }
683
684 return rc;
685bail:
686 if (pmic->irq_chip) {
687 pm8xxx_irq_exit(pmic->irq_chip);
688 pmic->irq_chip = NULL;
689 }
690 return rc;
691}
692
Anirudh Ghayalf1f1e142011-10-10 17:47:45 +0530693static int __devinit pm8058_probe(struct platform_device *pdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700694{
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530695 int rc;
696 struct pm8058_platform_data *pdata = pdev->dev.platform_data;
697 struct pm8058_chip *pmic;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700698
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530699 if (pdata == NULL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700700 pr_err("%s: No platform_data or IRQ.\n", __func__);
701 return -ENODEV;
702 }
703
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530704 pmic = kzalloc(sizeof *pmic, GFP_KERNEL);
705 if (pmic == NULL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700706 pr_err("%s: kzalloc() failed.\n", __func__);
707 return -ENOMEM;
708 }
709
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530710 pmic->dev = &pdev->dev;
711
712 pm8058_drvdata.pm_chip_data = pmic;
713 platform_set_drvdata(pdev, &pm8058_drvdata);
714
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700715 /* Read PMIC chip revision */
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530716 rc = pm8058_readb(pmic->dev, PM8058_REG_REV, &pmic->revision);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700717 if (rc)
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530718 pr_err("%s: Failed on pm8058_readb for revision: rc=%d.\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700719 __func__, rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700720
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530721 pr_info("%s: PMIC revision: %X\n", __func__, pmic->revision);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700722
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530723 (void) memcpy((void *)&pmic->pdata, (const void *)pdata,
724 sizeof(pmic->pdata));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700725
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530726 rc = pm8058_add_subdevices(pdata, pmic);
727 if (rc) {
728 pr_err("Cannot add subdevices rc=%d\n", rc);
729 goto err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700730 }
731
Anirudh Ghayal0c72b842011-12-19 09:08:09 +0530732 rc = pm8xxx_hard_reset_config(PM8XXX_SHUTDOWN_ON_HARD_RESET);
Abhijeet Dharmapurikara4a3eaf2011-09-22 15:27:28 -0700733 if (rc < 0)
734 pr_err("%s: failed to config shutdown on hard reset: %d\n",
735 __func__, rc);
736
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700737 return 0;
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530738
739err:
740 mfd_remove_devices(pmic->dev);
741 platform_set_drvdata(pdev, NULL);
742 kfree(pmic);
743 return rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700744}
745
Anirudh Ghayalf1f1e142011-10-10 17:47:45 +0530746static int __devexit pm8058_remove(struct platform_device *pdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700747{
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530748 struct pm8xxx_drvdata *drvdata;
749 struct pm8058_chip *pmic = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700750
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530751 drvdata = platform_get_drvdata(pdev);
752 if (drvdata)
753 pmic = drvdata->pm_chip_data;
754 if (pmic) {
755 if (pmic->dev)
756 mfd_remove_devices(pmic->dev);
757 if (pmic->irq_chip)
758 pm8xxx_irq_exit(pmic->irq_chip);
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530759 kfree(pmic->mfd_regulators);
760 kfree(pmic);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700761 }
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530762 platform_set_drvdata(pdev, NULL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700763
764 return 0;
765}
766
Anirudh Ghayalf1f1e142011-10-10 17:47:45 +0530767static struct platform_driver pm8058_driver = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700768 .probe = pm8058_probe,
769 .remove = __devexit_p(pm8058_remove),
Anirudh Ghayalf1f1e142011-10-10 17:47:45 +0530770 .driver = {
771 .name = "pm8058-core",
772 .owner = THIS_MODULE,
773 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700774};
775
776static int __init pm8058_init(void)
777{
Anirudh Ghayalf1f1e142011-10-10 17:47:45 +0530778 return platform_driver_register(&pm8058_driver);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700779}
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530780postcore_initcall(pm8058_init);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700781
782static void __exit pm8058_exit(void)
783{
Anirudh Ghayalf1f1e142011-10-10 17:47:45 +0530784 platform_driver_unregister(&pm8058_driver);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700785}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700786module_exit(pm8058_exit);
787
788MODULE_LICENSE("GPL v2");
789MODULE_DESCRIPTION("PMIC8058 core driver");
790MODULE_VERSION("1.0");
791MODULE_ALIAS("platform:pmic8058-core");