blob: 7ecd73eb2f84710cdd52eb24a11711fb69725db6 [file] [log] [blame]
Duy Truong790f06d2013-02-13 16:38:12 -08001/* Copyright (c) 2009-2011, The Linux Foundation. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
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>
Steve Mucklef132c6c2012-06-06 18:30:57 -070026#include <linux/module.h>
Anirudh Ghayalc2019332011-11-12 06:29:10 +053027
28#define REG_MPP_BASE 0x50
Anirudh Ghayalca42c7de2011-11-21 10:42:07 +053029#define REG_IRQ_BASE 0x1BB
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070030
31/* PMIC8058 Revision */
Anirudh Ghayalc2019332011-11-12 06:29:10 +053032#define PM8058_REG_REV 0x002 /* PMIC4 revision */
33#define PM8058_VERSION_MASK 0xF0
34#define PM8058_REVISION_MASK 0x0F
35#define PM8058_VERSION_VALUE 0xE0
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070036
Anirudh Ghayalc2019332011-11-12 06:29:10 +053037/* PMIC 8058 Battery Alarm SSBI registers */
38#define REG_BATT_ALARM_THRESH 0x023
39#define REG_BATT_ALARM_CTRL1 0x024
40#define REG_BATT_ALARM_CTRL2 0x0AA
41#define REG_BATT_ALARM_PWM_CTRL 0x0A3
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070042
Anirudh Ghayalc2019332011-11-12 06:29:10 +053043#define REG_TEMP_ALRM_CTRL 0x1B
44#define REG_TEMP_ALRM_PWM 0x9B
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070045
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070046/* PON CNTL 4 register */
47#define SSBI_REG_ADDR_PON_CNTL_4 0x98
48#define PM8058_PON_RESET_EN_MASK 0x01
49
50/* PON CNTL 5 register */
51#define SSBI_REG_ADDR_PON_CNTL_5 0x7B
52#define PM8058_HARD_RESET_EN_MASK 0x08
53
Willie Ruan6a3c9142011-07-14 16:52:41 -070054/* GP_TEST1 register */
55#define SSBI_REG_ADDR_GP_TEST_1 0x07A
56
Anirudh Ghayalc2019332011-11-12 06:29:10 +053057#define PM8058_RTC_BASE 0x1E8
58#define PM8058_OTHC_CNTR_BASE0 0xA0
59#define PM8058_OTHC_CNTR_BASE1 0x134
60#define PM8058_OTHC_CNTR_BASE2 0x137
61
62#define SINGLE_IRQ_RESOURCE(_name, _irq) \
63{ \
64 .name = _name, \
65 .start = _irq, \
66 .end = _irq, \
67 .flags = IORESOURCE_IRQ, \
68}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070069
70struct pm8058_chip {
71 struct pm8058_platform_data pdata;
Anirudh Ghayalf1f1e142011-10-10 17:47:45 +053072 struct device *dev;
Anirudh Ghayalc2019332011-11-12 06:29:10 +053073 struct pm_irq_chip *irq_chip;
74 struct mfd_cell *mfd_regulators, *mfd_xo_buffers;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070075
Anirudh Ghayalc2019332011-11-12 06:29:10 +053076 u8 revision;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070077};
78
Anirudh Ghayalc2019332011-11-12 06:29:10 +053079static int pm8058_readb(const struct device *dev, u16 addr, u8 *val)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070080{
Anirudh Ghayalc2019332011-11-12 06:29:10 +053081 const struct pm8xxx_drvdata *pm8058_drvdata = dev_get_drvdata(dev);
82 const struct pm8058_chip *pmic = pm8058_drvdata->pm_chip_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070083
Anirudh Ghayalc2019332011-11-12 06:29:10 +053084 return msm_ssbi_read(pmic->dev->parent, addr, val, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070085}
86
Anirudh Ghayalc2019332011-11-12 06:29:10 +053087static int pm8058_writeb(const struct device *dev, u16 addr, u8 val)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070088{
Anirudh Ghayalc2019332011-11-12 06:29:10 +053089 const struct pm8xxx_drvdata *pm8058_drvdata = dev_get_drvdata(dev);
90 const struct pm8058_chip *pmic = pm8058_drvdata->pm_chip_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070091
Anirudh Ghayalc2019332011-11-12 06:29:10 +053092 return msm_ssbi_write(pmic->dev->parent, addr, &val, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070093}
94
Anirudh Ghayalc2019332011-11-12 06:29:10 +053095static int pm8058_read_buf(const struct device *dev, u16 addr, u8 *buf,
96 int cnt)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070097{
Anirudh Ghayalc2019332011-11-12 06:29:10 +053098 const struct pm8xxx_drvdata *pm8058_drvdata = dev_get_drvdata(dev);
99 const struct pm8058_chip *pmic = pm8058_drvdata->pm_chip_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700100
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530101 return msm_ssbi_read(pmic->dev->parent, addr, buf, cnt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700102}
103
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530104static int pm8058_write_buf(const struct device *dev, u16 addr, u8 *buf,
105 int cnt)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700106{
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530107 const struct pm8xxx_drvdata *pm8058_drvdata = dev_get_drvdata(dev);
108 const struct pm8058_chip *pmic = pm8058_drvdata->pm_chip_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700109
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530110 return msm_ssbi_write(pmic->dev->parent, addr, buf, cnt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700111}
112
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530113static int pm8058_read_irq_stat(const struct device *dev, int irq)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700114{
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530115 const struct pm8xxx_drvdata *pm8058_drvdata = dev_get_drvdata(dev);
116 const struct pm8058_chip *pmic = pm8058_drvdata->pm_chip_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700117
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530118 return pm8xxx_get_irq_stat(pmic->irq_chip, irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700119
120 return 0;
121}
122
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530123static enum pm8xxx_version pm8058_get_version(const struct device *dev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700124{
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530125 const struct pm8xxx_drvdata *pm8058_drvdata = dev_get_drvdata(dev);
126 const struct pm8058_chip *pmic = pm8058_drvdata->pm_chip_data;
127 enum pm8xxx_version version = -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700128
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530129 if ((pmic->revision & PM8058_VERSION_MASK) == PM8058_VERSION_VALUE)
130 version = PM8XXX_VERSION_8058;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700131
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530132 return version;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700133}
134
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530135static int pm8058_get_revision(const struct device *dev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700136{
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530137 const struct pm8xxx_drvdata *pm8058_drvdata = dev_get_drvdata(dev);
138 const struct pm8058_chip *pmic = pm8058_drvdata->pm_chip_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700139
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530140 return pmic->revision & PM8058_REVISION_MASK;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700141}
142
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530143static struct pm8xxx_drvdata pm8058_drvdata = {
144 .pmic_readb = pm8058_readb,
145 .pmic_writeb = pm8058_writeb,
146 .pmic_read_buf = pm8058_read_buf,
147 .pmic_write_buf = pm8058_write_buf,
148 .pmic_read_irq_stat = pm8058_read_irq_stat,
149 .pmic_get_version = pm8058_get_version,
150 .pmic_get_revision = pm8058_get_revision,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700151};
152
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530153static const struct resource pm8058_charger_resources[] __devinitconst = {
154 SINGLE_IRQ_RESOURCE("CHGVAL", PM8058_CHGVAL_IRQ),
155 SINGLE_IRQ_RESOURCE("CHGINVAL", PM8058_CHGINVAL_IRQ),
156 SINGLE_IRQ_RESOURCE("CHGILIM", PM8058_CHGILIM_IRQ),
157 SINGLE_IRQ_RESOURCE("VCP", PM8058_VCP_IRQ),
158 SINGLE_IRQ_RESOURCE("ATC_DONE", PM8058_ATC_DONE_IRQ),
159 SINGLE_IRQ_RESOURCE("ATCFAIL", PM8058_ATCFAIL_IRQ),
160 SINGLE_IRQ_RESOURCE("AUTO_CHGDONE", PM8058_AUTO_CHGDONE_IRQ),
161 SINGLE_IRQ_RESOURCE("AUTO_CHGFAIL", PM8058_AUTO_CHGFAIL_IRQ),
162 SINGLE_IRQ_RESOURCE("CHGSTATE", PM8058_CHGSTATE_IRQ),
163 SINGLE_IRQ_RESOURCE("FASTCHG", PM8058_FASTCHG_IRQ),
164 SINGLE_IRQ_RESOURCE("CHG_END", PM8058_CHG_END_IRQ),
165 SINGLE_IRQ_RESOURCE("BATTTEMP", PM8058_BATTTEMP_IRQ),
166 SINGLE_IRQ_RESOURCE("CHGHOT", PM8058_CHGHOT_IRQ),
167 SINGLE_IRQ_RESOURCE("CHGTLIMIT", PM8058_CHGTLIMIT_IRQ),
168 SINGLE_IRQ_RESOURCE("CHG_GONE", PM8058_CHG_GONE_IRQ),
169 SINGLE_IRQ_RESOURCE("VCPMAJOR", PM8058_VCPMAJOR_IRQ),
170 SINGLE_IRQ_RESOURCE("VBATDET", PM8058_VBATDET_IRQ),
171 SINGLE_IRQ_RESOURCE("BATFET", PM8058_BATFET_IRQ),
172 SINGLE_IRQ_RESOURCE("BATT_REPLACE", PM8058_BATT_REPLACE_IRQ),
173 SINGLE_IRQ_RESOURCE("BATTCONNECT", PM8058_BATTCONNECT_IRQ),
174 SINGLE_IRQ_RESOURCE("VBATDET_LOW", PM8058_VBATDET_LOW_IRQ),
Abhijeet Dharmapurikara3c0d942011-07-25 17:53:45 -0700175};
176
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530177static struct mfd_cell pm8058_charger_cell __devinitdata = {
178 .name = "pm8058-charger",
179 .id = -1,
180 .resources = pm8058_charger_resources,
181 .num_resources = ARRAY_SIZE(pm8058_charger_resources),
182};
183
184static const struct resource misc_cell_resources[] __devinitconst = {
185 SINGLE_IRQ_RESOURCE("pm8xxx_osc_halt_irq", PM8058_OSCHALT_IRQ),
186};
187
188static struct mfd_cell misc_cell __devinitdata = {
189 .name = PM8XXX_MISC_DEV_NAME,
190 .id = -1,
191 .resources = misc_cell_resources,
192 .num_resources = ARRAY_SIZE(misc_cell_resources),
193};
194
195static struct mfd_cell pm8058_pwm_cell __devinitdata = {
196 .name = "pm8058-pwm",
197 .id = -1,
198};
199
200static struct resource xoadc_resources[] = {
201 SINGLE_IRQ_RESOURCE(NULL, PM8058_ADC_IRQ),
202};
203
204static struct mfd_cell xoadc_cell __devinitdata = {
205 .name = "pm8058-xoadc",
206 .id = -1,
207 .resources = xoadc_resources,
208 .num_resources = ARRAY_SIZE(xoadc_resources),
209};
210
211static const struct resource thermal_alarm_cell_resources[] __devinitconst = {
212 SINGLE_IRQ_RESOURCE("pm8058_tempstat_irq", PM8058_TEMPSTAT_IRQ),
213 SINGLE_IRQ_RESOURCE("pm8058_overtemp_irq", PM8058_OVERTEMP_IRQ),
214};
215
216static struct pm8xxx_tm_core_data thermal_alarm_cdata = {
217 .adc_channel = CHANNEL_ADC_DIE_TEMP,
218 .adc_type = PM8XXX_TM_ADC_PM8058_ADC,
219 .reg_addr_temp_alarm_ctrl = REG_TEMP_ALRM_CTRL,
220 .reg_addr_temp_alarm_pwm = REG_TEMP_ALRM_PWM,
221 .tm_name = "pm8058_tz",
222 .irq_name_temp_stat = "pm8058_tempstat_irq",
223 .irq_name_over_temp = "pm8058_overtemp_irq",
224};
225
226static struct mfd_cell thermal_alarm_cell __devinitdata = {
227 .name = PM8XXX_TM_DEV_NAME,
228 .id = -1,
229 .resources = thermal_alarm_cell_resources,
230 .num_resources = ARRAY_SIZE(thermal_alarm_cell_resources),
231 .platform_data = &thermal_alarm_cdata,
232 .pdata_size = sizeof(struct pm8xxx_tm_core_data),
233};
234
235static struct mfd_cell debugfs_cell __devinitdata = {
236 .name = "pm8xxx-debug",
237 .id = -1,
238 .platform_data = "pm8058-dbg",
239 .pdata_size = sizeof("pm8058-dbg"),
240};
241
242static const struct resource othc0_cell_resources[] __devinitconst = {
243 {
244 .name = "othc_base",
245 .start = PM8058_OTHC_CNTR_BASE0,
246 .end = PM8058_OTHC_CNTR_BASE0,
247 .flags = IORESOURCE_IO,
248 },
249};
250
251static const struct resource othc1_cell_resources[] __devinitconst = {
252 SINGLE_IRQ_RESOURCE(NULL, PM8058_SW_1_IRQ),
253 SINGLE_IRQ_RESOURCE(NULL, PM8058_IR_1_IRQ),
254 {
255 .name = "othc_base",
256 .start = PM8058_OTHC_CNTR_BASE1,
257 .end = PM8058_OTHC_CNTR_BASE1,
258 .flags = IORESOURCE_IO,
259 },
260};
261
262static const struct resource othc2_cell_resources[] __devinitconst = {
263 {
264 .name = "othc_base",
265 .start = PM8058_OTHC_CNTR_BASE2,
266 .end = PM8058_OTHC_CNTR_BASE2,
267 .flags = IORESOURCE_IO,
268 },
269};
270
271static const struct resource batt_alarm_cell_resources[] __devinitconst = {
272 SINGLE_IRQ_RESOURCE("pm8058_batt_alarm_irq", PM8058_BATT_ALARM_IRQ),
273};
274
275static struct mfd_cell leds_cell __devinitdata = {
276 .name = "pm8058-led",
277 .id = -1,
278};
279
280static struct mfd_cell othc0_cell __devinitdata = {
281 .name = "pm8058-othc",
282 .id = 0,
283 .resources = othc0_cell_resources,
284 .num_resources = ARRAY_SIZE(othc0_cell_resources),
285};
286
287static struct mfd_cell othc1_cell __devinitdata = {
288 .name = "pm8058-othc",
289 .id = 1,
290 .resources = othc1_cell_resources,
291 .num_resources = ARRAY_SIZE(othc1_cell_resources),
292};
293
294static struct mfd_cell othc2_cell __devinitdata = {
295 .name = "pm8058-othc",
296 .id = 2,
297 .resources = othc2_cell_resources,
298 .num_resources = ARRAY_SIZE(othc2_cell_resources),
299};
300
301static struct pm8xxx_batt_alarm_core_data batt_alarm_cdata = {
302 .irq_name = "pm8058_batt_alarm_irq",
303 .reg_addr_threshold = REG_BATT_ALARM_THRESH,
304 .reg_addr_ctrl1 = REG_BATT_ALARM_CTRL1,
305 .reg_addr_ctrl2 = REG_BATT_ALARM_CTRL2,
306 .reg_addr_pwm_ctrl = REG_BATT_ALARM_PWM_CTRL,
307};
308
309static struct mfd_cell batt_alarm_cell __devinitdata = {
310 .name = PM8XXX_BATT_ALARM_DEV_NAME,
311 .id = -1,
312 .resources = batt_alarm_cell_resources,
313 .num_resources = ARRAY_SIZE(batt_alarm_cell_resources),
314 .platform_data = &batt_alarm_cdata,
315 .pdata_size = sizeof(struct pm8xxx_batt_alarm_core_data),
316};
317
318static struct mfd_cell upl_cell __devinitdata = {
319 .name = PM8XXX_UPL_DEV_NAME,
320 .id = -1,
321};
322
323static struct mfd_cell nfc_cell __devinitdata = {
324 .name = PM8XXX_NFC_DEV_NAME,
325 .id = -1,
326};
327
328static const struct resource rtc_cell_resources[] __devinitconst = {
329 [0] = SINGLE_IRQ_RESOURCE(NULL, PM8058_RTC_ALARM_IRQ),
330 [1] = {
331 .name = "pmic_rtc_base",
332 .start = PM8058_RTC_BASE,
333 .end = PM8058_RTC_BASE,
334 .flags = IORESOURCE_IO,
335 },
336};
337
338static struct mfd_cell rtc_cell __devinitdata = {
339 .name = PM8XXX_RTC_DEV_NAME,
340 .id = -1,
341 .resources = rtc_cell_resources,
342 .num_resources = ARRAY_SIZE(rtc_cell_resources),
343};
344
345static const struct resource resources_pwrkey[] __devinitconst = {
346 SINGLE_IRQ_RESOURCE(NULL, PM8058_PWRKEY_REL_IRQ),
347 SINGLE_IRQ_RESOURCE(NULL, PM8058_PWRKEY_PRESS_IRQ),
348};
349
350static struct mfd_cell vibrator_cell __devinitdata = {
351 .name = PM8XXX_VIBRATOR_DEV_NAME,
352 .id = -1,
353};
354
355static struct mfd_cell pwrkey_cell __devinitdata = {
356 .name = PM8XXX_PWRKEY_DEV_NAME,
357 .id = -1,
358 .num_resources = ARRAY_SIZE(resources_pwrkey),
359 .resources = resources_pwrkey,
360};
361
362static const struct resource resources_keypad[] = {
363 SINGLE_IRQ_RESOURCE(NULL, PM8058_KEYPAD_IRQ),
364 SINGLE_IRQ_RESOURCE(NULL, PM8058_KEYSTUCK_IRQ),
365};
366
367static struct mfd_cell keypad_cell __devinitdata = {
368 .name = PM8XXX_KEYPAD_DEV_NAME,
369 .id = -1,
370 .num_resources = ARRAY_SIZE(resources_keypad),
371 .resources = resources_keypad,
372};
373
374static const struct resource mpp_cell_resources[] __devinitconst = {
375 {
376 .start = PM8058_IRQ_BLOCK_BIT(PM8058_MPP_BLOCK_START, 0),
377 .end = PM8058_IRQ_BLOCK_BIT(PM8058_MPP_BLOCK_START, 0)
378 + PM8058_MPPS - 1,
379 .flags = IORESOURCE_IRQ,
380 },
381};
382
383static struct mfd_cell mpp_cell __devinitdata = {
384 .name = PM8XXX_MPP_DEV_NAME,
385 .id = 0,
386 .resources = mpp_cell_resources,
387 .num_resources = ARRAY_SIZE(mpp_cell_resources),
388};
389
390static const struct resource gpio_cell_resources[] __devinitconst = {
391 [0] = {
392 .start = PM8058_IRQ_BLOCK_BIT(PM8058_GPIO_BLOCK_START, 0),
393 .end = PM8058_IRQ_BLOCK_BIT(PM8058_GPIO_BLOCK_START, 0)
394 + PM8058_GPIOS - 1,
395 .flags = IORESOURCE_IRQ,
396 },
397};
398
399static struct mfd_cell gpio_cell __devinitdata = {
400 .name = PM8XXX_GPIO_DEV_NAME,
401 .id = -1,
402 .resources = gpio_cell_resources,
403 .num_resources = ARRAY_SIZE(gpio_cell_resources),
404};
405
406static int __devinit
407pm8058_add_subdevices(const struct pm8058_platform_data *pdata,
408 struct pm8058_chip *pmic)
409{
410 int rc = 0, irq_base = 0, i;
411 struct pm_irq_chip *irq_chip;
412 static struct mfd_cell *mfd_regulators, *mfd_xo_buffers;
413
414 if (pdata->irq_pdata) {
415 pdata->irq_pdata->irq_cdata.nirqs = PM8058_NR_IRQS;
Anirudh Ghayalca42c7de2011-11-21 10:42:07 +0530416 pdata->irq_pdata->irq_cdata.base_addr = REG_IRQ_BASE;
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530417 irq_base = pdata->irq_pdata->irq_base;
418 irq_chip = pm8xxx_irq_init(pmic->dev, pdata->irq_pdata);
419
420 if (IS_ERR(irq_chip)) {
421 pr_err("Failed to init interrupts ret=%ld\n",
422 PTR_ERR(irq_chip));
423 return PTR_ERR(irq_chip);
424 }
425 pmic->irq_chip = irq_chip;
426 }
427
428 if (pdata->gpio_pdata) {
429 pdata->gpio_pdata->gpio_cdata.ngpios = PM8058_GPIOS;
430 gpio_cell.platform_data = pdata->gpio_pdata;
431 gpio_cell.pdata_size = sizeof(struct pm8xxx_gpio_platform_data);
432 rc = mfd_add_devices(pmic->dev, 0, &gpio_cell, 1,
433 NULL, irq_base);
434 if (rc) {
435 pr_err("Failed to add gpio subdevice ret=%d\n", rc);
436 goto bail;
437 }
438 }
439
440 if (pdata->mpp_pdata) {
441 pdata->mpp_pdata->core_data.nmpps = PM8058_MPPS;
442 pdata->mpp_pdata->core_data.base_addr = REG_MPP_BASE;
443 mpp_cell.platform_data = pdata->mpp_pdata;
444 mpp_cell.pdata_size = sizeof(struct pm8xxx_mpp_platform_data);
445 rc = mfd_add_devices(pmic->dev, 0, &mpp_cell, 1, NULL,
446 irq_base);
447 if (rc) {
448 pr_err("Failed to add mpp subdevice ret=%d\n", rc);
449 goto bail;
450 }
451 }
452
453 if (pdata->num_regulators > 0 && pdata->regulator_pdatas) {
454 mfd_regulators = kzalloc(sizeof(struct mfd_cell)
455 * (pdata->num_regulators), GFP_KERNEL);
456 if (!mfd_regulators) {
457 pr_err("Cannot allocate %d bytes for pm8058 regulator "
458 "mfd cells\n", sizeof(struct mfd_cell)
459 * (pdata->num_regulators));
460 rc = -ENOMEM;
461 goto bail;
462 }
463 for (i = 0; i < pdata->num_regulators; i++) {
464 mfd_regulators[i].name = "pm8058-regulator";
465 mfd_regulators[i].id = pdata->regulator_pdatas[i].id;
466 mfd_regulators[i].platform_data =
467 &(pdata->regulator_pdatas[i]);
468 mfd_regulators[i].pdata_size =
469 sizeof(struct pm8058_vreg_pdata);
470 }
471 rc = mfd_add_devices(pmic->dev, 0, mfd_regulators,
472 pdata->num_regulators, NULL, irq_base);
473 if (rc) {
474 pr_err("Failed to add regulator subdevices ret=%d\n",
475 rc);
476 kfree(mfd_regulators);
477 goto bail;
478 }
479 pmic->mfd_regulators = mfd_regulators;
480 }
481
482 if (pdata->num_xo_buffers > 0 && pdata->xo_buffer_pdata) {
483 mfd_xo_buffers = kzalloc(sizeof(struct mfd_cell)
484 * (pdata->num_xo_buffers), GFP_KERNEL);
485 if (!mfd_xo_buffers) {
486 pr_err("Cannot allocate %d bytes for pm8058 XO buffer "
487 "mfd cells\n", sizeof(struct mfd_cell)
488 * (pdata->num_xo_buffers));
489 rc = -ENOMEM;
490 goto bail;
491 }
492 for (i = 0; i < pdata->num_xo_buffers; i++) {
493 mfd_xo_buffers[i].name = PM8058_XO_BUFFER_DEV_NAME;
494 mfd_xo_buffers[i].id = pdata->xo_buffer_pdata[i].id;
495 mfd_xo_buffers[i].platform_data =
496 &(pdata->xo_buffer_pdata[i]);
497 mfd_xo_buffers[i].pdata_size =
498 sizeof(struct pm8058_xo_pdata);
499 }
500 rc = mfd_add_devices(pmic->dev, 0, mfd_xo_buffers,
501 pdata->num_xo_buffers, NULL, irq_base);
502 if (rc) {
503 pr_err("Failed to add XO buffer subdevices ret=%d\n",
504 rc);
505 kfree(mfd_xo_buffers);
506 goto bail;
507 }
508 pmic->mfd_xo_buffers = mfd_xo_buffers;
509 }
510
511 if (pdata->keypad_pdata) {
512 keypad_cell.platform_data = pdata->keypad_pdata;
513 keypad_cell.pdata_size =
514 sizeof(struct pm8xxx_keypad_platform_data);
515 rc = mfd_add_devices(pmic->dev, 0, &keypad_cell, 1, NULL,
516 irq_base);
517 if (rc) {
518 pr_err("Failed to add keypad subdevice ret=%d\n", rc);
519 goto bail;
520 }
521 }
522
523 if (pdata->rtc_pdata) {
524 rtc_cell.platform_data = pdata->rtc_pdata;
525 rtc_cell.pdata_size = sizeof(struct pm8xxx_rtc_platform_data);
526 rc = mfd_add_devices(pmic->dev, 0, &rtc_cell, 1, NULL,
527 irq_base);
528 if (rc) {
529 pr_err("Failed to add rtc subdevice ret=%d\n", rc);
530 goto bail;
531 }
532 }
533
534 if (pdata->pwrkey_pdata) {
535 pwrkey_cell.platform_data = pdata->pwrkey_pdata;
536 pwrkey_cell.pdata_size =
537 sizeof(struct pm8xxx_pwrkey_platform_data);
538 rc = mfd_add_devices(pmic->dev, 0, &pwrkey_cell, 1, NULL,
539 irq_base);
540 if (rc) {
541 pr_err("Failed to add pwrkey subdevice ret=%d\n", rc);
542 goto bail;
543 }
544 }
545
546 if (pdata->vibrator_pdata) {
547 vibrator_cell.platform_data = pdata->vibrator_pdata;
548 vibrator_cell.pdata_size =
549 sizeof(struct pm8xxx_vibrator_platform_data);
550 rc = mfd_add_devices(pmic->dev, 0, &vibrator_cell, 1, NULL,
551 irq_base);
552 if (rc) {
553 pr_err("Failed to add vibrator subdevice ret=%d\n",
554 rc);
555 goto bail;
556 }
557 }
558
559 if (pdata->leds_pdata) {
560 leds_cell.platform_data = pdata->leds_pdata;
561 leds_cell.pdata_size =
562 sizeof(struct pmic8058_leds_platform_data);
563 rc = mfd_add_devices(pmic->dev, 0, &leds_cell, 1, NULL,
564 irq_base);
565 if (rc) {
566 pr_err("Failed to add leds subdevice ret=%d\n", rc);
567 goto bail;
568 }
569 }
570
571 if (pdata->xoadc_pdata) {
572 xoadc_cell.platform_data = pdata->xoadc_pdata;
573 xoadc_cell.pdata_size =
574 sizeof(struct xoadc_platform_data);
575 rc = mfd_add_devices(pmic->dev, 0, &xoadc_cell, 1, NULL,
576 irq_base);
577 if (rc) {
578 pr_err("Failed to add leds subdevice ret=%d\n", rc);
579 goto bail;
580 }
581 }
582
583 if (pdata->othc0_pdata) {
584 othc0_cell.platform_data = pdata->othc0_pdata;
585 othc0_cell.pdata_size =
586 sizeof(struct pmic8058_othc_config_pdata);
587 rc = mfd_add_devices(pmic->dev, 0, &othc0_cell, 1, NULL, 0);
588 if (rc) {
589 pr_err("Failed to add othc0 subdevice ret=%d\n", rc);
590 goto bail;
591 }
592 }
593
594 if (pdata->othc1_pdata) {
595 othc1_cell.platform_data = pdata->othc1_pdata;
596 othc1_cell.pdata_size =
597 sizeof(struct pmic8058_othc_config_pdata);
598 rc = mfd_add_devices(pmic->dev, 0, &othc1_cell, 1, NULL,
599 irq_base);
600 if (rc) {
601 pr_err("Failed to add othc1 subdevice ret=%d\n", rc);
602 goto bail;
603 }
604 }
605
606 if (pdata->othc2_pdata) {
607 othc2_cell.platform_data = pdata->othc2_pdata;
608 othc2_cell.pdata_size =
609 sizeof(struct pmic8058_othc_config_pdata);
610 rc = mfd_add_devices(pmic->dev, 0, &othc2_cell, 1, NULL, 0);
611 if (rc) {
612 pr_err("Failed to add othc2 subdevice ret=%d\n", rc);
613 goto bail;
614 }
615 }
616
617 if (pdata->pwm_pdata) {
618 pm8058_pwm_cell.platform_data = pdata->pwm_pdata;
619 pm8058_pwm_cell.pdata_size = sizeof(struct pm8058_pwm_pdata);
620 rc = mfd_add_devices(pmic->dev, 0, &pm8058_pwm_cell, 1, NULL,
621 irq_base);
622 if (rc) {
623 pr_err("Failed to add pwm subdevice ret=%d\n", rc);
624 goto bail;
625 }
626 }
627
628 if (pdata->misc_pdata) {
629 misc_cell.platform_data = pdata->misc_pdata;
630 misc_cell.pdata_size = sizeof(struct pm8xxx_misc_platform_data);
631 rc = mfd_add_devices(pmic->dev, 0, &misc_cell, 1, NULL,
632 irq_base);
633 if (rc) {
634 pr_err("Failed to add misc subdevice ret=%d\n", rc);
635 goto bail;
636 }
637 }
638
639 rc = mfd_add_devices(pmic->dev, 0, &thermal_alarm_cell, 1, NULL,
640 irq_base);
641 if (rc) {
642 pr_err("Failed to add thermal alarm subdevice ret=%d\n",
643 rc);
644 goto bail;
645 }
646
647 rc = mfd_add_devices(pmic->dev, 0, &batt_alarm_cell, 1, NULL,
648 irq_base);
649 if (rc) {
650 pr_err("Failed to add battery alarm subdevice ret=%d\n",
651 rc);
652 goto bail;
653 }
654
655 rc = mfd_add_devices(pmic->dev, 0, &upl_cell, 1, NULL, 0);
656 if (rc) {
657 pr_err("Failed to add upl subdevice ret=%d\n", rc);
658 goto bail;
659 }
660
661 rc = mfd_add_devices(pmic->dev, 0, &nfc_cell, 1, NULL, 0);
662 if (rc) {
663 pr_err("Failed to add upl subdevice ret=%d\n", rc);
664 goto bail;
665 }
666
667 if (pdata->charger_pdata) {
668 pm8058_charger_cell.platform_data = pdata->charger_pdata;
669 pm8058_charger_cell.pdata_size = sizeof(struct
670 pmic8058_charger_data);
671 rc = mfd_add_devices(pmic->dev, 0, &pm8058_charger_cell,
672 1, NULL, irq_base);
673 if (rc) {
674 pr_err("Failed to add charger subdevice ret=%d\n", rc);
675 goto bail;
676 }
677 }
678
679 rc = mfd_add_devices(pmic->dev, 0, &debugfs_cell, 1, NULL, irq_base);
680 if (rc) {
681 pr_err("Failed to add debugfs subdevice ret=%d\n", rc);
682 goto bail;
683 }
684
685 return rc;
686bail:
687 if (pmic->irq_chip) {
688 pm8xxx_irq_exit(pmic->irq_chip);
689 pmic->irq_chip = NULL;
690 }
691 return rc;
692}
693
Anirudh Ghayalf1f1e142011-10-10 17:47:45 +0530694static int __devinit pm8058_probe(struct platform_device *pdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700695{
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530696 int rc;
697 struct pm8058_platform_data *pdata = pdev->dev.platform_data;
698 struct pm8058_chip *pmic;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700699
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530700 if (pdata == NULL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700701 pr_err("%s: No platform_data or IRQ.\n", __func__);
702 return -ENODEV;
703 }
704
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530705 pmic = kzalloc(sizeof *pmic, GFP_KERNEL);
706 if (pmic == NULL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700707 pr_err("%s: kzalloc() failed.\n", __func__);
708 return -ENOMEM;
709 }
710
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530711 pmic->dev = &pdev->dev;
712
713 pm8058_drvdata.pm_chip_data = pmic;
714 platform_set_drvdata(pdev, &pm8058_drvdata);
715
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700716 /* Read PMIC chip revision */
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530717 rc = pm8058_readb(pmic->dev, PM8058_REG_REV, &pmic->revision);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700718 if (rc)
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530719 pr_err("%s: Failed on pm8058_readb for revision: rc=%d.\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700720 __func__, rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700721
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530722 pr_info("%s: PMIC revision: %X\n", __func__, pmic->revision);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700723
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530724 (void) memcpy((void *)&pmic->pdata, (const void *)pdata,
725 sizeof(pmic->pdata));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700726
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530727 rc = pm8058_add_subdevices(pdata, pmic);
728 if (rc) {
729 pr_err("Cannot add subdevices rc=%d\n", rc);
730 goto err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700731 }
732
Anirudh Ghayal0c72b842011-12-19 09:08:09 +0530733 rc = pm8xxx_hard_reset_config(PM8XXX_SHUTDOWN_ON_HARD_RESET);
Abhijeet Dharmapurikara4a3eaf2011-09-22 15:27:28 -0700734 if (rc < 0)
735 pr_err("%s: failed to config shutdown on hard reset: %d\n",
736 __func__, rc);
737
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700738 return 0;
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530739
740err:
741 mfd_remove_devices(pmic->dev);
742 platform_set_drvdata(pdev, NULL);
743 kfree(pmic);
744 return rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700745}
746
Anirudh Ghayalf1f1e142011-10-10 17:47:45 +0530747static int __devexit pm8058_remove(struct platform_device *pdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700748{
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530749 struct pm8xxx_drvdata *drvdata;
750 struct pm8058_chip *pmic = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700751
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530752 drvdata = platform_get_drvdata(pdev);
753 if (drvdata)
754 pmic = drvdata->pm_chip_data;
755 if (pmic) {
756 if (pmic->dev)
757 mfd_remove_devices(pmic->dev);
758 if (pmic->irq_chip)
759 pm8xxx_irq_exit(pmic->irq_chip);
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530760 kfree(pmic->mfd_regulators);
761 kfree(pmic);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700762 }
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530763 platform_set_drvdata(pdev, NULL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700764
765 return 0;
766}
767
Anirudh Ghayalf1f1e142011-10-10 17:47:45 +0530768static struct platform_driver pm8058_driver = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700769 .probe = pm8058_probe,
770 .remove = __devexit_p(pm8058_remove),
Anirudh Ghayalf1f1e142011-10-10 17:47:45 +0530771 .driver = {
772 .name = "pm8058-core",
773 .owner = THIS_MODULE,
774 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700775};
776
777static int __init pm8058_init(void)
778{
Anirudh Ghayalf1f1e142011-10-10 17:47:45 +0530779 return platform_driver_register(&pm8058_driver);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700780}
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530781postcore_initcall(pm8058_init);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700782
783static void __exit pm8058_exit(void)
784{
Anirudh Ghayalf1f1e142011-10-10 17:47:45 +0530785 platform_driver_unregister(&pm8058_driver);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700786}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700787module_exit(pm8058_exit);
788
789MODULE_LICENSE("GPL v2");
790MODULE_DESCRIPTION("PMIC8058 core driver");
791MODULE_VERSION("1.0");
792MODULE_ALIAS("platform:pmic8058-core");