blob: 77e393eb8ccef7d573ef3260437094a62b4b79ca [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
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070028
29/* PMIC8058 Revision */
Anirudh Ghayalc2019332011-11-12 06:29:10 +053030#define PM8058_REG_REV 0x002 /* PMIC4 revision */
31#define PM8058_VERSION_MASK 0xF0
32#define PM8058_REVISION_MASK 0x0F
33#define PM8058_VERSION_VALUE 0xE0
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070034
Anirudh Ghayalc2019332011-11-12 06:29:10 +053035/* PMIC 8058 Battery Alarm SSBI registers */
36#define REG_BATT_ALARM_THRESH 0x023
37#define REG_BATT_ALARM_CTRL1 0x024
38#define REG_BATT_ALARM_CTRL2 0x0AA
39#define REG_BATT_ALARM_PWM_CTRL 0x0A3
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070040
Anirudh Ghayalc2019332011-11-12 06:29:10 +053041#define REG_TEMP_ALRM_CTRL 0x1B
42#define REG_TEMP_ALRM_PWM 0x9B
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070043
44/* PON CNTL 1 register */
45#define SSBI_REG_ADDR_PON_CNTL_1 0x01C
46
47#define PM8058_PON_PUP_MASK 0xF0
48
49#define PM8058_PON_WD_EN_MASK 0x08
50#define PM8058_PON_WD_EN_RESET 0x08
51#define PM8058_PON_WD_EN_PWR_OFF 0x00
52
53/* PON CNTL 4 register */
54#define SSBI_REG_ADDR_PON_CNTL_4 0x98
55#define PM8058_PON_RESET_EN_MASK 0x01
56
57/* PON CNTL 5 register */
58#define SSBI_REG_ADDR_PON_CNTL_5 0x7B
59#define PM8058_HARD_RESET_EN_MASK 0x08
60
David Collins0a911602011-06-15 15:03:13 -070061/* Regulator master enable addresses */
62#define SSBI_REG_ADDR_VREG_EN_MSM 0x018
63#define SSBI_REG_ADDR_VREG_EN_GRP_5_4 0x1C8
64
65/* Regulator control registers for shutdown/reset */
66#define SSBI_REG_ADDR_S0_CTRL 0x004
67#define SSBI_REG_ADDR_S1_CTRL 0x005
68#define SSBI_REG_ADDR_S3_CTRL 0x111
69#define SSBI_REG_ADDR_L21_CTRL 0x120
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070070#define SSBI_REG_ADDR_L22_CTRL 0x121
71
David Collins0a911602011-06-15 15:03:13 -070072#define REGULATOR_ENABLE_MASK 0x80
73#define REGULATOR_ENABLE 0x80
74#define REGULATOR_DISABLE 0x00
75#define REGULATOR_PULL_DOWN_MASK 0x40
76#define REGULATOR_PULL_DOWN_EN 0x40
77#define REGULATOR_PULL_DOWN_DIS 0x00
78
79/* Buck CTRL register */
80#define SMPS_LEGACY_VREF_SEL 0x20
81#define SMPS_LEGACY_VPROG_MASK 0x1F
82#define SMPS_ADVANCED_BAND_MASK 0xC0
83#define SMPS_ADVANCED_BAND_SHIFT 6
84#define SMPS_ADVANCED_VPROG_MASK 0x3F
85
86/* Buck TEST2 registers for shutdown/reset */
87#define SSBI_REG_ADDR_S0_TEST2 0x084
88#define SSBI_REG_ADDR_S1_TEST2 0x085
89#define SSBI_REG_ADDR_S3_TEST2 0x11A
90
91#define REGULATOR_BANK_WRITE 0x80
92#define REGULATOR_BANK_MASK 0x70
93#define REGULATOR_BANK_SHIFT 4
94#define REGULATOR_BANK_SEL(n) ((n) << REGULATOR_BANK_SHIFT)
95
96/* Buck TEST2 register bank 1 */
97#define SMPS_LEGACY_VLOW_SEL 0x01
98
99/* Buck TEST2 register bank 7 */
100#define SMPS_ADVANCED_MODE_MASK 0x02
101#define SMPS_ADVANCED_MODE 0x02
102#define SMPS_LEGACY_MODE 0x00
103
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700104/* SLEEP CNTL register */
105#define SSBI_REG_ADDR_SLEEP_CNTL 0x02B
106
107#define PM8058_SLEEP_SMPL_EN_MASK 0x04
108#define PM8058_SLEEP_SMPL_EN_RESET 0x04
109#define PM8058_SLEEP_SMPL_EN_PWR_OFF 0x00
110
111#define PM8058_SLEEP_SMPL_SEL_MASK 0x03
112#define PM8058_SLEEP_SMPL_SEL_MIN 0
113#define PM8058_SLEEP_SMPL_SEL_MAX 3
114
Willie Ruan6a3c9142011-07-14 16:52:41 -0700115/* GP_TEST1 register */
116#define SSBI_REG_ADDR_GP_TEST_1 0x07A
117
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530118#define PM8058_RTC_BASE 0x1E8
119#define PM8058_OTHC_CNTR_BASE0 0xA0
120#define PM8058_OTHC_CNTR_BASE1 0x134
121#define PM8058_OTHC_CNTR_BASE2 0x137
122
123#define SINGLE_IRQ_RESOURCE(_name, _irq) \
124{ \
125 .name = _name, \
126 .start = _irq, \
127 .end = _irq, \
128 .flags = IORESOURCE_IRQ, \
129}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700130
131struct pm8058_chip {
132 struct pm8058_platform_data pdata;
Anirudh Ghayalf1f1e142011-10-10 17:47:45 +0530133 struct device *dev;
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530134 struct pm_irq_chip *irq_chip;
135 struct mfd_cell *mfd_regulators, *mfd_xo_buffers;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700136
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530137 u8 revision;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700138
139 struct mutex pm_lock;
140};
141
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700142static struct pm8058_chip *pmic_chip;
143
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700144static inline int
Anirudh Ghayalf1f1e142011-10-10 17:47:45 +0530145ssbi_read(struct device *dev, u16 addr, u8 *buf, size_t len)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700146{
Anirudh Ghayalf1f1e142011-10-10 17:47:45 +0530147 return msm_ssbi_read(dev->parent, addr, buf, len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700148}
149
150static inline int
Anirudh Ghayalf1f1e142011-10-10 17:47:45 +0530151ssbi_write(struct device *dev, u16 addr, u8 *buf, size_t len)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700152{
Anirudh Ghayalf1f1e142011-10-10 17:47:45 +0530153 return msm_ssbi_write(dev->parent, addr, buf, len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700154}
155
156static int pm8058_masked_write(u16 addr, u8 val, u8 mask)
157{
158 int rc;
159 u8 reg;
160
161 if (pmic_chip == NULL)
162 return -ENODEV;
163
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700164 rc = ssbi_read(pmic_chip->dev, addr, &reg, 1);
165 if (rc) {
166 pr_err("%s: ssbi_read(0x%03X) failed: rc=%d\n", __func__, addr,
167 rc);
168 goto done;
169 }
170
171 reg &= ~mask;
172 reg |= val & mask;
173
174 rc = ssbi_write(pmic_chip->dev, addr, &reg, 1);
175 if (rc)
176 pr_err("%s: ssbi_write(0x%03X)=0x%02X failed: rc=%d\n",
177 __func__, addr, reg, rc);
178done:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700179 return rc;
180}
181
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700182/**
183 * pm8058_smpl_control - enables/disables SMPL detection
184 * @enable: 0 = shutdown PMIC on power loss, 1 = reset PMIC on power loss
185 *
186 * This function enables or disables the Sudden Momentary Power Loss detection
187 * module. If SMPL detection is enabled, then when a sufficiently long power
188 * loss event occurs, the PMIC will automatically reset itself. If SMPL
189 * detection is disabled, then the PMIC will shutdown when power loss occurs.
190 *
191 * RETURNS: an appropriate -ERRNO error value on error, or zero for success.
192 */
193int pm8058_smpl_control(int enable)
194{
195 return pm8058_masked_write(SSBI_REG_ADDR_SLEEP_CNTL,
196 (enable ? PM8058_SLEEP_SMPL_EN_RESET
197 : PM8058_SLEEP_SMPL_EN_PWR_OFF),
198 PM8058_SLEEP_SMPL_EN_MASK);
199}
200EXPORT_SYMBOL(pm8058_smpl_control);
201
202/**
203 * pm8058_smpl_set_delay - sets the SMPL detection time delay
204 * @delay: enum value corresponding to delay time
205 *
206 * This function sets the time delay of the SMPL detection module. If power
207 * is reapplied within this interval, then the PMIC reset automatically. The
208 * SMPL detection module must be enabled for this delay time to take effect.
209 *
210 * RETURNS: an appropriate -ERRNO error value on error, or zero for success.
211 */
212int pm8058_smpl_set_delay(enum pm8058_smpl_delay delay)
213{
214 if (delay < PM8058_SLEEP_SMPL_SEL_MIN
215 || delay > PM8058_SLEEP_SMPL_SEL_MAX) {
216 pr_err("%s: invalid delay specified: %d\n", __func__, delay);
217 return -EINVAL;
218 }
219
220 return pm8058_masked_write(SSBI_REG_ADDR_SLEEP_CNTL, delay,
221 PM8058_SLEEP_SMPL_SEL_MASK);
222}
223EXPORT_SYMBOL(pm8058_smpl_set_delay);
224
225/**
226 * pm8058_watchdog_reset_control - enables/disables watchdog reset detection
227 * @enable: 0 = shutdown when PS_HOLD goes low, 1 = reset when PS_HOLD goes low
228 *
229 * This function enables or disables the PMIC watchdog reset detection feature.
230 * If watchdog reset detection is enabled, then the PMIC will reset itself
231 * when PS_HOLD goes low. If it is not enabled, then the PMIC will shutdown
232 * when PS_HOLD goes low.
233 *
234 * RETURNS: an appropriate -ERRNO error value on error, or zero for success.
235 */
236int pm8058_watchdog_reset_control(int enable)
237{
238 return pm8058_masked_write(SSBI_REG_ADDR_PON_CNTL_1,
239 (enable ? PM8058_PON_WD_EN_RESET
240 : PM8058_PON_WD_EN_PWR_OFF),
241 PM8058_PON_WD_EN_MASK);
242}
243EXPORT_SYMBOL(pm8058_watchdog_reset_control);
244
David Collins0a911602011-06-15 15:03:13 -0700245/*
246 * Set an SMPS regulator to be disabled in its CTRL register, but enabled
247 * in the master enable register. Also set it's pull down enable bit.
248 * Take care to make sure that the output voltage doesn't change if switching
249 * from advanced mode to legacy mode.
250 */
251static int disable_smps_locally_set_pull_down(u16 ctrl_addr, u16 test2_addr,
252 u16 master_enable_addr, u8 master_enable_bit)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700253{
David Collins0a911602011-06-15 15:03:13 -0700254 int rc = 0;
255 u8 vref_sel, vlow_sel, band, vprog, bank, reg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700256
257 if (pmic_chip == NULL)
258 return -ENODEV;
259
David Collins0a911602011-06-15 15:03:13 -0700260 bank = REGULATOR_BANK_SEL(7);
261 rc = ssbi_write(pmic_chip->dev, test2_addr, &bank, 1);
262 if (rc) {
263 pr_err("%s: FAIL ssbi_write(0x%03X): rc=%d\n", __func__,
264 test2_addr, rc);
265 goto done;
266 }
267
268 rc = ssbi_read(pmic_chip->dev, test2_addr, &reg, 1);
269 if (rc) {
270 pr_err("%s: FAIL pm8058_read(0x%03X): rc=%d\n",
271 __func__, test2_addr, rc);
272 goto done;
273 }
274
275 /* Check if in advanced mode. */
276 if ((reg & SMPS_ADVANCED_MODE_MASK) == SMPS_ADVANCED_MODE) {
277 /* Determine current output voltage. */
278 rc = ssbi_read(pmic_chip->dev, ctrl_addr, &reg, 1);
279 if (rc) {
280 pr_err("%s: FAIL pm8058_read(0x%03X): rc=%d\n",
281 __func__, ctrl_addr, rc);
282 goto done;
283 }
284
285 band = (reg & SMPS_ADVANCED_BAND_MASK)
286 >> SMPS_ADVANCED_BAND_SHIFT;
287 switch (band) {
288 case 3:
289 vref_sel = 0;
290 vlow_sel = 0;
291 break;
292 case 2:
293 vref_sel = SMPS_LEGACY_VREF_SEL;
294 vlow_sel = 0;
295 break;
296 case 1:
297 vref_sel = SMPS_LEGACY_VREF_SEL;
298 vlow_sel = SMPS_LEGACY_VLOW_SEL;
299 break;
300 default:
301 pr_err("%s: regulator already disabled\n", __func__);
302 return -EPERM;
303 }
304 vprog = (reg & SMPS_ADVANCED_VPROG_MASK);
305 /* Round up if fine step is in use. */
306 vprog = (vprog + 1) >> 1;
307 if (vprog > SMPS_LEGACY_VPROG_MASK)
308 vprog = SMPS_LEGACY_VPROG_MASK;
309
310 /* Set VLOW_SEL bit. */
311 bank = REGULATOR_BANK_SEL(1);
312 rc = ssbi_write(pmic_chip->dev, test2_addr, &bank, 1);
313 if (rc) {
314 pr_err("%s: FAIL ssbi_write(0x%03X): rc=%d\n",
315 __func__, test2_addr, rc);
316 goto done;
317 }
318 rc = pm8058_masked_write(test2_addr,
319 REGULATOR_BANK_WRITE | REGULATOR_BANK_SEL(1)
320 | vlow_sel,
321 REGULATOR_BANK_WRITE | REGULATOR_BANK_MASK
322 | SMPS_LEGACY_VLOW_SEL);
323 if (rc)
324 goto done;
325
326 /* Switch to legacy mode */
327 bank = REGULATOR_BANK_SEL(7);
328 rc = ssbi_write(pmic_chip->dev, test2_addr, &bank, 1);
329 if (rc) {
330 pr_err("%s: FAIL ssbi_write(0x%03X): rc=%d\n", __func__,
331 test2_addr, rc);
332 goto done;
333 }
334 rc = pm8058_masked_write(test2_addr,
335 REGULATOR_BANK_WRITE | REGULATOR_BANK_SEL(7)
336 | SMPS_LEGACY_MODE,
337 REGULATOR_BANK_WRITE | REGULATOR_BANK_MASK
338 | SMPS_ADVANCED_MODE_MASK);
339 if (rc)
340 goto done;
341
342 /* Enable locally, enable pull down, keep voltage the same. */
343 rc = pm8058_masked_write(ctrl_addr,
344 REGULATOR_ENABLE | REGULATOR_PULL_DOWN_EN
345 | vref_sel | vprog,
346 REGULATOR_ENABLE_MASK | REGULATOR_PULL_DOWN_MASK
347 | SMPS_LEGACY_VREF_SEL | SMPS_LEGACY_VPROG_MASK);
348 if (rc)
349 goto done;
350 }
351
352 /* Enable in master control register. */
353 rc = pm8058_masked_write(master_enable_addr, master_enable_bit,
354 master_enable_bit);
355 if (rc)
356 goto done;
357
358 /* Disable locally and enable pull down. */
359 rc = pm8058_masked_write(ctrl_addr,
360 REGULATOR_DISABLE | REGULATOR_PULL_DOWN_EN,
361 REGULATOR_ENABLE_MASK | REGULATOR_PULL_DOWN_MASK);
362
363done:
364 return rc;
365}
366
367static int disable_ldo_locally_set_pull_down(u16 ctrl_addr,
368 u16 master_enable_addr, u8 master_enable_bit)
369{
370 int rc;
371
372 /* Enable LDO in master control register. */
373 rc = pm8058_masked_write(master_enable_addr, master_enable_bit,
374 master_enable_bit);
375 if (rc)
376 goto done;
377
378 /* Disable LDO in CTRL register and set pull down */
379 rc = pm8058_masked_write(ctrl_addr,
380 REGULATOR_DISABLE | REGULATOR_PULL_DOWN_EN,
381 REGULATOR_ENABLE_MASK | REGULATOR_PULL_DOWN_MASK);
382
383done:
384 return rc;
385}
386
387int pm8058_reset_pwr_off(int reset)
388{
389 int rc;
390 u8 pon, ctrl, smpl;
391
392 if (pmic_chip == NULL)
393 return -ENODEV;
394
395 /* When shutting down, enable active pulldowns on important rails. */
396 if (!reset) {
397 /* Disable SMPS's 0,1,3 locally and set pulldown enable bits. */
398 disable_smps_locally_set_pull_down(SSBI_REG_ADDR_S0_CTRL,
399 SSBI_REG_ADDR_S0_TEST2, SSBI_REG_ADDR_VREG_EN_MSM, BIT(7));
400 disable_smps_locally_set_pull_down(SSBI_REG_ADDR_S1_CTRL,
401 SSBI_REG_ADDR_S1_TEST2, SSBI_REG_ADDR_VREG_EN_MSM, BIT(6));
402 disable_smps_locally_set_pull_down(SSBI_REG_ADDR_S3_CTRL,
403 SSBI_REG_ADDR_S3_TEST2, SSBI_REG_ADDR_VREG_EN_GRP_5_4,
404 BIT(7) | BIT(4));
405 /* Disable LDO 21 locally and set pulldown enable bit. */
406 disable_ldo_locally_set_pull_down(SSBI_REG_ADDR_L21_CTRL,
407 SSBI_REG_ADDR_VREG_EN_GRP_5_4, BIT(1));
408 }
409
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700410 /* Set regulator L22 to 1.225V in high power mode. */
411 rc = ssbi_read(pmic_chip->dev, SSBI_REG_ADDR_L22_CTRL, &ctrl, 1);
412 if (rc) {
413 pr_err("%s: FAIL ssbi_read(0x%x): rc=%d\n", __func__,
414 SSBI_REG_ADDR_L22_CTRL, rc);
415 goto get_out3;
416 }
417 /* Leave pull-down state intact. */
418 ctrl &= 0x40;
419 ctrl |= 0x93;
420 rc = ssbi_write(pmic_chip->dev, SSBI_REG_ADDR_L22_CTRL, &ctrl, 1);
421 if (rc)
422 pr_err("%s: FAIL ssbi_write(0x%x)=0x%x: rc=%d\n", __func__,
423 SSBI_REG_ADDR_L22_CTRL, ctrl, rc);
424
425get_out3:
426 if (!reset) {
427 /* Only modify the SLEEP_CNTL reg if shutdown is desired. */
428 rc = ssbi_read(pmic_chip->dev, SSBI_REG_ADDR_SLEEP_CNTL,
429 &smpl, 1);
430 if (rc) {
431 pr_err("%s: FAIL ssbi_read(0x%x): rc=%d\n",
432 __func__, SSBI_REG_ADDR_SLEEP_CNTL, rc);
433 goto get_out2;
434 }
435
436 smpl &= ~PM8058_SLEEP_SMPL_EN_MASK;
437 smpl |= PM8058_SLEEP_SMPL_EN_PWR_OFF;
438
439 rc = ssbi_write(pmic_chip->dev, SSBI_REG_ADDR_SLEEP_CNTL,
440 &smpl, 1);
441 if (rc)
442 pr_err("%s: FAIL ssbi_write(0x%x)=0x%x: rc=%d\n",
443 __func__, SSBI_REG_ADDR_SLEEP_CNTL, smpl, rc);
444 }
445
446get_out2:
447 rc = ssbi_read(pmic_chip->dev, SSBI_REG_ADDR_PON_CNTL_1, &pon, 1);
448 if (rc) {
449 pr_err("%s: FAIL ssbi_read(0x%x): rc=%d\n",
450 __func__, SSBI_REG_ADDR_PON_CNTL_1, rc);
451 goto get_out;
452 }
453
454 pon &= ~PM8058_PON_WD_EN_MASK;
455 pon |= reset ? PM8058_PON_WD_EN_RESET : PM8058_PON_WD_EN_PWR_OFF;
456
457 /* Enable all pullups */
458 pon |= PM8058_PON_PUP_MASK;
459
460 rc = ssbi_write(pmic_chip->dev, SSBI_REG_ADDR_PON_CNTL_1, &pon, 1);
461 if (rc) {
462 pr_err("%s: FAIL ssbi_write(0x%x)=0x%x: rc=%d\n",
463 __func__, SSBI_REG_ADDR_PON_CNTL_1, pon, rc);
464 goto get_out;
465 }
466
467get_out:
468 return rc;
469}
470EXPORT_SYMBOL(pm8058_reset_pwr_off);
471
Willie Ruan6a3c9142011-07-14 16:52:41 -0700472/**
473 * pm8058_stay_on - enables stay_on feature
474 *
475 * PMIC stay-on feature allows PMIC to ignore MSM PS_HOLD=low
476 * signal so that some special functions like debugging could be
477 * performed.
478 *
479 * This feature should not be used in any product release.
480 *
481 * RETURNS: an appropriate -ERRNO error value on error, or zero for success.
482 */
483int pm8058_stay_on(void)
484{
485 u8 ctrl = 0x92;
486 int rc;
487
488 rc = ssbi_write(pmic_chip->dev, SSBI_REG_ADDR_GP_TEST_1, &ctrl, 1);
489 pr_info("%s: set stay-on: rc = %d\n", __func__, rc);
490 return rc;
491}
492EXPORT_SYMBOL(pm8058_stay_on);
493
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700494/*
495 power on hard reset configuration
496 config = DISABLE_HARD_RESET to disable hard reset
497 = SHUTDOWN_ON_HARD_RESET to turn off the system on hard reset
498 = RESTART_ON_HARD_RESET to restart the system on hard reset
499 */
500int pm8058_hard_reset_config(enum pon_config config)
501{
502 int rc, ret;
503 u8 pon, pon_5;
504
505 if (config >= MAX_PON_CONFIG)
506 return -EINVAL;
507
508 if (pmic_chip == NULL)
509 return -ENODEV;
510
511 mutex_lock(&pmic_chip->pm_lock);
512
513 rc = ssbi_read(pmic_chip->dev, SSBI_REG_ADDR_PON_CNTL_5, &pon, 1);
514 if (rc) {
515 pr_err("%s: FAIL ssbi_read(0x%x): rc=%d\n",
516 __func__, SSBI_REG_ADDR_PON_CNTL_5, rc);
517 mutex_unlock(&pmic_chip->pm_lock);
518 return rc;
519 }
520
521 pon_5 = pon;
522 (config != DISABLE_HARD_RESET) ? (pon |= PM8058_HARD_RESET_EN_MASK) :
523 (pon &= ~PM8058_HARD_RESET_EN_MASK);
524
525 rc = ssbi_write(pmic_chip->dev, SSBI_REG_ADDR_PON_CNTL_5, &pon, 1);
526 if (rc) {
527 pr_err("%s: FAIL ssbi_write(0x%x)=0x%x: rc=%d\n",
528 __func__, SSBI_REG_ADDR_PON_CNTL_5, pon, rc);
529 mutex_unlock(&pmic_chip->pm_lock);
530 return rc;
531 }
532
533 if (config == DISABLE_HARD_RESET) {
534 mutex_unlock(&pmic_chip->pm_lock);
535 return 0;
536 }
537
538 rc = ssbi_read(pmic_chip->dev, SSBI_REG_ADDR_PON_CNTL_4, &pon, 1);
539 if (rc) {
540 pr_err("%s: FAIL ssbi_read(0x%x): rc=%d\n",
541 __func__, SSBI_REG_ADDR_PON_CNTL_4, rc);
542 goto err_restore_pon_5;
543 }
544
545 (config == RESTART_ON_HARD_RESET) ? (pon |= PM8058_PON_RESET_EN_MASK) :
546 (pon &= ~PM8058_PON_RESET_EN_MASK);
547
548 rc = ssbi_write(pmic_chip->dev, SSBI_REG_ADDR_PON_CNTL_4, &pon, 1);
549 if (rc) {
550 pr_err("%s: FAIL ssbi_write(0x%x)=0x%x: rc=%d\n",
551 __func__, SSBI_REG_ADDR_PON_CNTL_4, pon, rc);
552 goto err_restore_pon_5;
553 }
554 mutex_unlock(&pmic_chip->pm_lock);
555 return 0;
556
557err_restore_pon_5:
558 ret = ssbi_write(pmic_chip->dev, SSBI_REG_ADDR_PON_CNTL_5, &pon_5, 1);
559 if (ret)
560 pr_err("%s: FAIL ssbi_write(0x%x)=0x%x: rc=%d\n",
561 __func__, SSBI_REG_ADDR_PON_CNTL_5, pon, ret);
562 mutex_unlock(&pmic_chip->pm_lock);
563 return rc;
564}
565EXPORT_SYMBOL(pm8058_hard_reset_config);
566
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530567static int pm8058_readb(const struct device *dev, u16 addr, u8 *val)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700568{
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530569 const struct pm8xxx_drvdata *pm8058_drvdata = dev_get_drvdata(dev);
570 const struct pm8058_chip *pmic = pm8058_drvdata->pm_chip_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700571
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530572 return msm_ssbi_read(pmic->dev->parent, addr, val, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700573}
574
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530575static int pm8058_writeb(const struct device *dev, u16 addr, u8 val)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700576{
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530577 const struct pm8xxx_drvdata *pm8058_drvdata = dev_get_drvdata(dev);
578 const struct pm8058_chip *pmic = pm8058_drvdata->pm_chip_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700579
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530580 return msm_ssbi_write(pmic->dev->parent, addr, &val, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700581}
582
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530583static int pm8058_read_buf(const struct device *dev, u16 addr, u8 *buf,
584 int cnt)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700585{
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530586 const struct pm8xxx_drvdata *pm8058_drvdata = dev_get_drvdata(dev);
587 const struct pm8058_chip *pmic = pm8058_drvdata->pm_chip_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700588
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530589 return msm_ssbi_read(pmic->dev->parent, addr, buf, cnt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700590}
591
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530592static int pm8058_write_buf(const struct device *dev, u16 addr, u8 *buf,
593 int cnt)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700594{
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530595 const struct pm8xxx_drvdata *pm8058_drvdata = dev_get_drvdata(dev);
596 const struct pm8058_chip *pmic = pm8058_drvdata->pm_chip_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700597
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530598 return msm_ssbi_write(pmic->dev->parent, addr, buf, cnt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700599}
600
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530601static int pm8058_read_irq_stat(const struct device *dev, int irq)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700602{
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530603 const struct pm8xxx_drvdata *pm8058_drvdata = dev_get_drvdata(dev);
604 const struct pm8058_chip *pmic = pm8058_drvdata->pm_chip_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700605
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530606 return pm8xxx_get_irq_stat(pmic->irq_chip, irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700607
608 return 0;
609}
610
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530611static enum pm8xxx_version pm8058_get_version(const struct device *dev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700612{
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530613 const struct pm8xxx_drvdata *pm8058_drvdata = dev_get_drvdata(dev);
614 const struct pm8058_chip *pmic = pm8058_drvdata->pm_chip_data;
615 enum pm8xxx_version version = -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700616
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530617 if ((pmic->revision & PM8058_VERSION_MASK) == PM8058_VERSION_VALUE)
618 version = PM8XXX_VERSION_8058;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700619
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530620 return version;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700621}
622
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530623static int pm8058_get_revision(const struct device *dev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700624{
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530625 const struct pm8xxx_drvdata *pm8058_drvdata = dev_get_drvdata(dev);
626 const struct pm8058_chip *pmic = pm8058_drvdata->pm_chip_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700627
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530628 return pmic->revision & PM8058_REVISION_MASK;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700629}
630
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530631static struct pm8xxx_drvdata pm8058_drvdata = {
632 .pmic_readb = pm8058_readb,
633 .pmic_writeb = pm8058_writeb,
634 .pmic_read_buf = pm8058_read_buf,
635 .pmic_write_buf = pm8058_write_buf,
636 .pmic_read_irq_stat = pm8058_read_irq_stat,
637 .pmic_get_version = pm8058_get_version,
638 .pmic_get_revision = pm8058_get_revision,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700639};
640
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530641static const struct resource pm8058_charger_resources[] __devinitconst = {
642 SINGLE_IRQ_RESOURCE("CHGVAL", PM8058_CHGVAL_IRQ),
643 SINGLE_IRQ_RESOURCE("CHGINVAL", PM8058_CHGINVAL_IRQ),
644 SINGLE_IRQ_RESOURCE("CHGILIM", PM8058_CHGILIM_IRQ),
645 SINGLE_IRQ_RESOURCE("VCP", PM8058_VCP_IRQ),
646 SINGLE_IRQ_RESOURCE("ATC_DONE", PM8058_ATC_DONE_IRQ),
647 SINGLE_IRQ_RESOURCE("ATCFAIL", PM8058_ATCFAIL_IRQ),
648 SINGLE_IRQ_RESOURCE("AUTO_CHGDONE", PM8058_AUTO_CHGDONE_IRQ),
649 SINGLE_IRQ_RESOURCE("AUTO_CHGFAIL", PM8058_AUTO_CHGFAIL_IRQ),
650 SINGLE_IRQ_RESOURCE("CHGSTATE", PM8058_CHGSTATE_IRQ),
651 SINGLE_IRQ_RESOURCE("FASTCHG", PM8058_FASTCHG_IRQ),
652 SINGLE_IRQ_RESOURCE("CHG_END", PM8058_CHG_END_IRQ),
653 SINGLE_IRQ_RESOURCE("BATTTEMP", PM8058_BATTTEMP_IRQ),
654 SINGLE_IRQ_RESOURCE("CHGHOT", PM8058_CHGHOT_IRQ),
655 SINGLE_IRQ_RESOURCE("CHGTLIMIT", PM8058_CHGTLIMIT_IRQ),
656 SINGLE_IRQ_RESOURCE("CHG_GONE", PM8058_CHG_GONE_IRQ),
657 SINGLE_IRQ_RESOURCE("VCPMAJOR", PM8058_VCPMAJOR_IRQ),
658 SINGLE_IRQ_RESOURCE("VBATDET", PM8058_VBATDET_IRQ),
659 SINGLE_IRQ_RESOURCE("BATFET", PM8058_BATFET_IRQ),
660 SINGLE_IRQ_RESOURCE("BATT_REPLACE", PM8058_BATT_REPLACE_IRQ),
661 SINGLE_IRQ_RESOURCE("BATTCONNECT", PM8058_BATTCONNECT_IRQ),
662 SINGLE_IRQ_RESOURCE("VBATDET_LOW", PM8058_VBATDET_LOW_IRQ),
Abhijeet Dharmapurikara3c0d942011-07-25 17:53:45 -0700663};
664
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530665static struct mfd_cell pm8058_charger_cell __devinitdata = {
666 .name = "pm8058-charger",
667 .id = -1,
668 .resources = pm8058_charger_resources,
669 .num_resources = ARRAY_SIZE(pm8058_charger_resources),
670};
671
672static const struct resource misc_cell_resources[] __devinitconst = {
673 SINGLE_IRQ_RESOURCE("pm8xxx_osc_halt_irq", PM8058_OSCHALT_IRQ),
674};
675
676static struct mfd_cell misc_cell __devinitdata = {
677 .name = PM8XXX_MISC_DEV_NAME,
678 .id = -1,
679 .resources = misc_cell_resources,
680 .num_resources = ARRAY_SIZE(misc_cell_resources),
681};
682
683static struct mfd_cell pm8058_pwm_cell __devinitdata = {
684 .name = "pm8058-pwm",
685 .id = -1,
686};
687
688static struct resource xoadc_resources[] = {
689 SINGLE_IRQ_RESOURCE(NULL, PM8058_ADC_IRQ),
690};
691
692static struct mfd_cell xoadc_cell __devinitdata = {
693 .name = "pm8058-xoadc",
694 .id = -1,
695 .resources = xoadc_resources,
696 .num_resources = ARRAY_SIZE(xoadc_resources),
697};
698
699static const struct resource thermal_alarm_cell_resources[] __devinitconst = {
700 SINGLE_IRQ_RESOURCE("pm8058_tempstat_irq", PM8058_TEMPSTAT_IRQ),
701 SINGLE_IRQ_RESOURCE("pm8058_overtemp_irq", PM8058_OVERTEMP_IRQ),
702};
703
704static struct pm8xxx_tm_core_data thermal_alarm_cdata = {
705 .adc_channel = CHANNEL_ADC_DIE_TEMP,
706 .adc_type = PM8XXX_TM_ADC_PM8058_ADC,
707 .reg_addr_temp_alarm_ctrl = REG_TEMP_ALRM_CTRL,
708 .reg_addr_temp_alarm_pwm = REG_TEMP_ALRM_PWM,
709 .tm_name = "pm8058_tz",
710 .irq_name_temp_stat = "pm8058_tempstat_irq",
711 .irq_name_over_temp = "pm8058_overtemp_irq",
712};
713
714static struct mfd_cell thermal_alarm_cell __devinitdata = {
715 .name = PM8XXX_TM_DEV_NAME,
716 .id = -1,
717 .resources = thermal_alarm_cell_resources,
718 .num_resources = ARRAY_SIZE(thermal_alarm_cell_resources),
719 .platform_data = &thermal_alarm_cdata,
720 .pdata_size = sizeof(struct pm8xxx_tm_core_data),
721};
722
723static struct mfd_cell debugfs_cell __devinitdata = {
724 .name = "pm8xxx-debug",
725 .id = -1,
726 .platform_data = "pm8058-dbg",
727 .pdata_size = sizeof("pm8058-dbg"),
728};
729
730static const struct resource othc0_cell_resources[] __devinitconst = {
731 {
732 .name = "othc_base",
733 .start = PM8058_OTHC_CNTR_BASE0,
734 .end = PM8058_OTHC_CNTR_BASE0,
735 .flags = IORESOURCE_IO,
736 },
737};
738
739static const struct resource othc1_cell_resources[] __devinitconst = {
740 SINGLE_IRQ_RESOURCE(NULL, PM8058_SW_1_IRQ),
741 SINGLE_IRQ_RESOURCE(NULL, PM8058_IR_1_IRQ),
742 {
743 .name = "othc_base",
744 .start = PM8058_OTHC_CNTR_BASE1,
745 .end = PM8058_OTHC_CNTR_BASE1,
746 .flags = IORESOURCE_IO,
747 },
748};
749
750static const struct resource othc2_cell_resources[] __devinitconst = {
751 {
752 .name = "othc_base",
753 .start = PM8058_OTHC_CNTR_BASE2,
754 .end = PM8058_OTHC_CNTR_BASE2,
755 .flags = IORESOURCE_IO,
756 },
757};
758
759static const struct resource batt_alarm_cell_resources[] __devinitconst = {
760 SINGLE_IRQ_RESOURCE("pm8058_batt_alarm_irq", PM8058_BATT_ALARM_IRQ),
761};
762
763static struct mfd_cell leds_cell __devinitdata = {
764 .name = "pm8058-led",
765 .id = -1,
766};
767
768static struct mfd_cell othc0_cell __devinitdata = {
769 .name = "pm8058-othc",
770 .id = 0,
771 .resources = othc0_cell_resources,
772 .num_resources = ARRAY_SIZE(othc0_cell_resources),
773};
774
775static struct mfd_cell othc1_cell __devinitdata = {
776 .name = "pm8058-othc",
777 .id = 1,
778 .resources = othc1_cell_resources,
779 .num_resources = ARRAY_SIZE(othc1_cell_resources),
780};
781
782static struct mfd_cell othc2_cell __devinitdata = {
783 .name = "pm8058-othc",
784 .id = 2,
785 .resources = othc2_cell_resources,
786 .num_resources = ARRAY_SIZE(othc2_cell_resources),
787};
788
789static struct pm8xxx_batt_alarm_core_data batt_alarm_cdata = {
790 .irq_name = "pm8058_batt_alarm_irq",
791 .reg_addr_threshold = REG_BATT_ALARM_THRESH,
792 .reg_addr_ctrl1 = REG_BATT_ALARM_CTRL1,
793 .reg_addr_ctrl2 = REG_BATT_ALARM_CTRL2,
794 .reg_addr_pwm_ctrl = REG_BATT_ALARM_PWM_CTRL,
795};
796
797static struct mfd_cell batt_alarm_cell __devinitdata = {
798 .name = PM8XXX_BATT_ALARM_DEV_NAME,
799 .id = -1,
800 .resources = batt_alarm_cell_resources,
801 .num_resources = ARRAY_SIZE(batt_alarm_cell_resources),
802 .platform_data = &batt_alarm_cdata,
803 .pdata_size = sizeof(struct pm8xxx_batt_alarm_core_data),
804};
805
806static struct mfd_cell upl_cell __devinitdata = {
807 .name = PM8XXX_UPL_DEV_NAME,
808 .id = -1,
809};
810
811static struct mfd_cell nfc_cell __devinitdata = {
812 .name = PM8XXX_NFC_DEV_NAME,
813 .id = -1,
814};
815
816static const struct resource rtc_cell_resources[] __devinitconst = {
817 [0] = SINGLE_IRQ_RESOURCE(NULL, PM8058_RTC_ALARM_IRQ),
818 [1] = {
819 .name = "pmic_rtc_base",
820 .start = PM8058_RTC_BASE,
821 .end = PM8058_RTC_BASE,
822 .flags = IORESOURCE_IO,
823 },
824};
825
826static struct mfd_cell rtc_cell __devinitdata = {
827 .name = PM8XXX_RTC_DEV_NAME,
828 .id = -1,
829 .resources = rtc_cell_resources,
830 .num_resources = ARRAY_SIZE(rtc_cell_resources),
831};
832
833static const struct resource resources_pwrkey[] __devinitconst = {
834 SINGLE_IRQ_RESOURCE(NULL, PM8058_PWRKEY_REL_IRQ),
835 SINGLE_IRQ_RESOURCE(NULL, PM8058_PWRKEY_PRESS_IRQ),
836};
837
838static struct mfd_cell vibrator_cell __devinitdata = {
839 .name = PM8XXX_VIBRATOR_DEV_NAME,
840 .id = -1,
841};
842
843static struct mfd_cell pwrkey_cell __devinitdata = {
844 .name = PM8XXX_PWRKEY_DEV_NAME,
845 .id = -1,
846 .num_resources = ARRAY_SIZE(resources_pwrkey),
847 .resources = resources_pwrkey,
848};
849
850static const struct resource resources_keypad[] = {
851 SINGLE_IRQ_RESOURCE(NULL, PM8058_KEYPAD_IRQ),
852 SINGLE_IRQ_RESOURCE(NULL, PM8058_KEYSTUCK_IRQ),
853};
854
855static struct mfd_cell keypad_cell __devinitdata = {
856 .name = PM8XXX_KEYPAD_DEV_NAME,
857 .id = -1,
858 .num_resources = ARRAY_SIZE(resources_keypad),
859 .resources = resources_keypad,
860};
861
862static const struct resource mpp_cell_resources[] __devinitconst = {
863 {
864 .start = PM8058_IRQ_BLOCK_BIT(PM8058_MPP_BLOCK_START, 0),
865 .end = PM8058_IRQ_BLOCK_BIT(PM8058_MPP_BLOCK_START, 0)
866 + PM8058_MPPS - 1,
867 .flags = IORESOURCE_IRQ,
868 },
869};
870
871static struct mfd_cell mpp_cell __devinitdata = {
872 .name = PM8XXX_MPP_DEV_NAME,
873 .id = 0,
874 .resources = mpp_cell_resources,
875 .num_resources = ARRAY_SIZE(mpp_cell_resources),
876};
877
878static const struct resource gpio_cell_resources[] __devinitconst = {
879 [0] = {
880 .start = PM8058_IRQ_BLOCK_BIT(PM8058_GPIO_BLOCK_START, 0),
881 .end = PM8058_IRQ_BLOCK_BIT(PM8058_GPIO_BLOCK_START, 0)
882 + PM8058_GPIOS - 1,
883 .flags = IORESOURCE_IRQ,
884 },
885};
886
887static struct mfd_cell gpio_cell __devinitdata = {
888 .name = PM8XXX_GPIO_DEV_NAME,
889 .id = -1,
890 .resources = gpio_cell_resources,
891 .num_resources = ARRAY_SIZE(gpio_cell_resources),
892};
893
894static int __devinit
895pm8058_add_subdevices(const struct pm8058_platform_data *pdata,
896 struct pm8058_chip *pmic)
897{
898 int rc = 0, irq_base = 0, i;
899 struct pm_irq_chip *irq_chip;
900 static struct mfd_cell *mfd_regulators, *mfd_xo_buffers;
901
902 if (pdata->irq_pdata) {
903 pdata->irq_pdata->irq_cdata.nirqs = PM8058_NR_IRQS;
904 irq_base = pdata->irq_pdata->irq_base;
905 irq_chip = pm8xxx_irq_init(pmic->dev, pdata->irq_pdata);
906
907 if (IS_ERR(irq_chip)) {
908 pr_err("Failed to init interrupts ret=%ld\n",
909 PTR_ERR(irq_chip));
910 return PTR_ERR(irq_chip);
911 }
912 pmic->irq_chip = irq_chip;
913 }
914
915 if (pdata->gpio_pdata) {
916 pdata->gpio_pdata->gpio_cdata.ngpios = PM8058_GPIOS;
917 gpio_cell.platform_data = pdata->gpio_pdata;
918 gpio_cell.pdata_size = sizeof(struct pm8xxx_gpio_platform_data);
919 rc = mfd_add_devices(pmic->dev, 0, &gpio_cell, 1,
920 NULL, irq_base);
921 if (rc) {
922 pr_err("Failed to add gpio subdevice ret=%d\n", rc);
923 goto bail;
924 }
925 }
926
927 if (pdata->mpp_pdata) {
928 pdata->mpp_pdata->core_data.nmpps = PM8058_MPPS;
929 pdata->mpp_pdata->core_data.base_addr = REG_MPP_BASE;
930 mpp_cell.platform_data = pdata->mpp_pdata;
931 mpp_cell.pdata_size = sizeof(struct pm8xxx_mpp_platform_data);
932 rc = mfd_add_devices(pmic->dev, 0, &mpp_cell, 1, NULL,
933 irq_base);
934 if (rc) {
935 pr_err("Failed to add mpp subdevice ret=%d\n", rc);
936 goto bail;
937 }
938 }
939
940 if (pdata->num_regulators > 0 && pdata->regulator_pdatas) {
941 mfd_regulators = kzalloc(sizeof(struct mfd_cell)
942 * (pdata->num_regulators), GFP_KERNEL);
943 if (!mfd_regulators) {
944 pr_err("Cannot allocate %d bytes for pm8058 regulator "
945 "mfd cells\n", sizeof(struct mfd_cell)
946 * (pdata->num_regulators));
947 rc = -ENOMEM;
948 goto bail;
949 }
950 for (i = 0; i < pdata->num_regulators; i++) {
951 mfd_regulators[i].name = "pm8058-regulator";
952 mfd_regulators[i].id = pdata->regulator_pdatas[i].id;
953 mfd_regulators[i].platform_data =
954 &(pdata->regulator_pdatas[i]);
955 mfd_regulators[i].pdata_size =
956 sizeof(struct pm8058_vreg_pdata);
957 }
958 rc = mfd_add_devices(pmic->dev, 0, mfd_regulators,
959 pdata->num_regulators, NULL, irq_base);
960 if (rc) {
961 pr_err("Failed to add regulator subdevices ret=%d\n",
962 rc);
963 kfree(mfd_regulators);
964 goto bail;
965 }
966 pmic->mfd_regulators = mfd_regulators;
967 }
968
969 if (pdata->num_xo_buffers > 0 && pdata->xo_buffer_pdata) {
970 mfd_xo_buffers = kzalloc(sizeof(struct mfd_cell)
971 * (pdata->num_xo_buffers), GFP_KERNEL);
972 if (!mfd_xo_buffers) {
973 pr_err("Cannot allocate %d bytes for pm8058 XO buffer "
974 "mfd cells\n", sizeof(struct mfd_cell)
975 * (pdata->num_xo_buffers));
976 rc = -ENOMEM;
977 goto bail;
978 }
979 for (i = 0; i < pdata->num_xo_buffers; i++) {
980 mfd_xo_buffers[i].name = PM8058_XO_BUFFER_DEV_NAME;
981 mfd_xo_buffers[i].id = pdata->xo_buffer_pdata[i].id;
982 mfd_xo_buffers[i].platform_data =
983 &(pdata->xo_buffer_pdata[i]);
984 mfd_xo_buffers[i].pdata_size =
985 sizeof(struct pm8058_xo_pdata);
986 }
987 rc = mfd_add_devices(pmic->dev, 0, mfd_xo_buffers,
988 pdata->num_xo_buffers, NULL, irq_base);
989 if (rc) {
990 pr_err("Failed to add XO buffer subdevices ret=%d\n",
991 rc);
992 kfree(mfd_xo_buffers);
993 goto bail;
994 }
995 pmic->mfd_xo_buffers = mfd_xo_buffers;
996 }
997
998 if (pdata->keypad_pdata) {
999 keypad_cell.platform_data = pdata->keypad_pdata;
1000 keypad_cell.pdata_size =
1001 sizeof(struct pm8xxx_keypad_platform_data);
1002 rc = mfd_add_devices(pmic->dev, 0, &keypad_cell, 1, NULL,
1003 irq_base);
1004 if (rc) {
1005 pr_err("Failed to add keypad subdevice ret=%d\n", rc);
1006 goto bail;
1007 }
1008 }
1009
1010 if (pdata->rtc_pdata) {
1011 rtc_cell.platform_data = pdata->rtc_pdata;
1012 rtc_cell.pdata_size = sizeof(struct pm8xxx_rtc_platform_data);
1013 rc = mfd_add_devices(pmic->dev, 0, &rtc_cell, 1, NULL,
1014 irq_base);
1015 if (rc) {
1016 pr_err("Failed to add rtc subdevice ret=%d\n", rc);
1017 goto bail;
1018 }
1019 }
1020
1021 if (pdata->pwrkey_pdata) {
1022 pwrkey_cell.platform_data = pdata->pwrkey_pdata;
1023 pwrkey_cell.pdata_size =
1024 sizeof(struct pm8xxx_pwrkey_platform_data);
1025 rc = mfd_add_devices(pmic->dev, 0, &pwrkey_cell, 1, NULL,
1026 irq_base);
1027 if (rc) {
1028 pr_err("Failed to add pwrkey subdevice ret=%d\n", rc);
1029 goto bail;
1030 }
1031 }
1032
1033 if (pdata->vibrator_pdata) {
1034 vibrator_cell.platform_data = pdata->vibrator_pdata;
1035 vibrator_cell.pdata_size =
1036 sizeof(struct pm8xxx_vibrator_platform_data);
1037 rc = mfd_add_devices(pmic->dev, 0, &vibrator_cell, 1, NULL,
1038 irq_base);
1039 if (rc) {
1040 pr_err("Failed to add vibrator subdevice ret=%d\n",
1041 rc);
1042 goto bail;
1043 }
1044 }
1045
1046 if (pdata->leds_pdata) {
1047 leds_cell.platform_data = pdata->leds_pdata;
1048 leds_cell.pdata_size =
1049 sizeof(struct pmic8058_leds_platform_data);
1050 rc = mfd_add_devices(pmic->dev, 0, &leds_cell, 1, NULL,
1051 irq_base);
1052 if (rc) {
1053 pr_err("Failed to add leds subdevice ret=%d\n", rc);
1054 goto bail;
1055 }
1056 }
1057
1058 if (pdata->xoadc_pdata) {
1059 xoadc_cell.platform_data = pdata->xoadc_pdata;
1060 xoadc_cell.pdata_size =
1061 sizeof(struct xoadc_platform_data);
1062 rc = mfd_add_devices(pmic->dev, 0, &xoadc_cell, 1, NULL,
1063 irq_base);
1064 if (rc) {
1065 pr_err("Failed to add leds subdevice ret=%d\n", rc);
1066 goto bail;
1067 }
1068 }
1069
1070 if (pdata->othc0_pdata) {
1071 othc0_cell.platform_data = pdata->othc0_pdata;
1072 othc0_cell.pdata_size =
1073 sizeof(struct pmic8058_othc_config_pdata);
1074 rc = mfd_add_devices(pmic->dev, 0, &othc0_cell, 1, NULL, 0);
1075 if (rc) {
1076 pr_err("Failed to add othc0 subdevice ret=%d\n", rc);
1077 goto bail;
1078 }
1079 }
1080
1081 if (pdata->othc1_pdata) {
1082 othc1_cell.platform_data = pdata->othc1_pdata;
1083 othc1_cell.pdata_size =
1084 sizeof(struct pmic8058_othc_config_pdata);
1085 rc = mfd_add_devices(pmic->dev, 0, &othc1_cell, 1, NULL,
1086 irq_base);
1087 if (rc) {
1088 pr_err("Failed to add othc1 subdevice ret=%d\n", rc);
1089 goto bail;
1090 }
1091 }
1092
1093 if (pdata->othc2_pdata) {
1094 othc2_cell.platform_data = pdata->othc2_pdata;
1095 othc2_cell.pdata_size =
1096 sizeof(struct pmic8058_othc_config_pdata);
1097 rc = mfd_add_devices(pmic->dev, 0, &othc2_cell, 1, NULL, 0);
1098 if (rc) {
1099 pr_err("Failed to add othc2 subdevice ret=%d\n", rc);
1100 goto bail;
1101 }
1102 }
1103
1104 if (pdata->pwm_pdata) {
1105 pm8058_pwm_cell.platform_data = pdata->pwm_pdata;
1106 pm8058_pwm_cell.pdata_size = sizeof(struct pm8058_pwm_pdata);
1107 rc = mfd_add_devices(pmic->dev, 0, &pm8058_pwm_cell, 1, NULL,
1108 irq_base);
1109 if (rc) {
1110 pr_err("Failed to add pwm subdevice ret=%d\n", rc);
1111 goto bail;
1112 }
1113 }
1114
1115 if (pdata->misc_pdata) {
1116 misc_cell.platform_data = pdata->misc_pdata;
1117 misc_cell.pdata_size = sizeof(struct pm8xxx_misc_platform_data);
1118 rc = mfd_add_devices(pmic->dev, 0, &misc_cell, 1, NULL,
1119 irq_base);
1120 if (rc) {
1121 pr_err("Failed to add misc subdevice ret=%d\n", rc);
1122 goto bail;
1123 }
1124 }
1125
1126 rc = mfd_add_devices(pmic->dev, 0, &thermal_alarm_cell, 1, NULL,
1127 irq_base);
1128 if (rc) {
1129 pr_err("Failed to add thermal alarm subdevice ret=%d\n",
1130 rc);
1131 goto bail;
1132 }
1133
1134 rc = mfd_add_devices(pmic->dev, 0, &batt_alarm_cell, 1, NULL,
1135 irq_base);
1136 if (rc) {
1137 pr_err("Failed to add battery alarm subdevice ret=%d\n",
1138 rc);
1139 goto bail;
1140 }
1141
1142 rc = mfd_add_devices(pmic->dev, 0, &upl_cell, 1, NULL, 0);
1143 if (rc) {
1144 pr_err("Failed to add upl subdevice ret=%d\n", rc);
1145 goto bail;
1146 }
1147
1148 rc = mfd_add_devices(pmic->dev, 0, &nfc_cell, 1, NULL, 0);
1149 if (rc) {
1150 pr_err("Failed to add upl subdevice ret=%d\n", rc);
1151 goto bail;
1152 }
1153
1154 if (pdata->charger_pdata) {
1155 pm8058_charger_cell.platform_data = pdata->charger_pdata;
1156 pm8058_charger_cell.pdata_size = sizeof(struct
1157 pmic8058_charger_data);
1158 rc = mfd_add_devices(pmic->dev, 0, &pm8058_charger_cell,
1159 1, NULL, irq_base);
1160 if (rc) {
1161 pr_err("Failed to add charger subdevice ret=%d\n", rc);
1162 goto bail;
1163 }
1164 }
1165
1166 rc = mfd_add_devices(pmic->dev, 0, &debugfs_cell, 1, NULL, irq_base);
1167 if (rc) {
1168 pr_err("Failed to add debugfs subdevice ret=%d\n", rc);
1169 goto bail;
1170 }
1171
1172 return rc;
1173bail:
1174 if (pmic->irq_chip) {
1175 pm8xxx_irq_exit(pmic->irq_chip);
1176 pmic->irq_chip = NULL;
1177 }
1178 return rc;
1179}
1180
Anirudh Ghayalf1f1e142011-10-10 17:47:45 +05301181static int __devinit pm8058_probe(struct platform_device *pdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001182{
Anirudh Ghayalc2019332011-11-12 06:29:10 +05301183 int rc;
1184 struct pm8058_platform_data *pdata = pdev->dev.platform_data;
1185 struct pm8058_chip *pmic;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001186
Anirudh Ghayalc2019332011-11-12 06:29:10 +05301187 if (pdata == NULL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001188 pr_err("%s: No platform_data or IRQ.\n", __func__);
1189 return -ENODEV;
1190 }
1191
Anirudh Ghayalc2019332011-11-12 06:29:10 +05301192 pmic = kzalloc(sizeof *pmic, GFP_KERNEL);
1193 if (pmic == NULL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001194 pr_err("%s: kzalloc() failed.\n", __func__);
1195 return -ENOMEM;
1196 }
1197
Anirudh Ghayalc2019332011-11-12 06:29:10 +05301198 pmic->dev = &pdev->dev;
1199
1200 pm8058_drvdata.pm_chip_data = pmic;
1201 platform_set_drvdata(pdev, &pm8058_drvdata);
1202
1203 mutex_init(&pmic->pm_lock);
1204 pmic_chip = pmic;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001205
1206 /* Read PMIC chip revision */
Anirudh Ghayalc2019332011-11-12 06:29:10 +05301207 rc = pm8058_readb(pmic->dev, PM8058_REG_REV, &pmic->revision);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001208 if (rc)
Anirudh Ghayalc2019332011-11-12 06:29:10 +05301209 pr_err("%s: Failed on pm8058_readb for revision: rc=%d.\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001210 __func__, rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001211
Anirudh Ghayalc2019332011-11-12 06:29:10 +05301212 pr_info("%s: PMIC revision: %X\n", __func__, pmic->revision);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001213
Anirudh Ghayalc2019332011-11-12 06:29:10 +05301214 (void) memcpy((void *)&pmic->pdata, (const void *)pdata,
1215 sizeof(pmic->pdata));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001216
Anirudh Ghayalc2019332011-11-12 06:29:10 +05301217 rc = pm8058_add_subdevices(pdata, pmic);
1218 if (rc) {
1219 pr_err("Cannot add subdevices rc=%d\n", rc);
1220 goto err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001221 }
1222
Abhijeet Dharmapurikara4a3eaf2011-09-22 15:27:28 -07001223 rc = pm8058_hard_reset_config(SHUTDOWN_ON_HARD_RESET);
1224 if (rc < 0)
1225 pr_err("%s: failed to config shutdown on hard reset: %d\n",
1226 __func__, rc);
1227
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001228 return 0;
Anirudh Ghayalc2019332011-11-12 06:29:10 +05301229
1230err:
1231 mfd_remove_devices(pmic->dev);
1232 platform_set_drvdata(pdev, NULL);
1233 kfree(pmic);
1234 return rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001235}
1236
Anirudh Ghayalf1f1e142011-10-10 17:47:45 +05301237static int __devexit pm8058_remove(struct platform_device *pdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001238{
Anirudh Ghayalc2019332011-11-12 06:29:10 +05301239 struct pm8xxx_drvdata *drvdata;
1240 struct pm8058_chip *pmic = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001241
Anirudh Ghayalc2019332011-11-12 06:29:10 +05301242 drvdata = platform_get_drvdata(pdev);
1243 if (drvdata)
1244 pmic = drvdata->pm_chip_data;
1245 if (pmic) {
1246 if (pmic->dev)
1247 mfd_remove_devices(pmic->dev);
1248 if (pmic->irq_chip)
1249 pm8xxx_irq_exit(pmic->irq_chip);
1250 mutex_destroy(&pmic->pm_lock);
1251 kfree(pmic->mfd_regulators);
1252 kfree(pmic);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001253 }
Anirudh Ghayalc2019332011-11-12 06:29:10 +05301254 platform_set_drvdata(pdev, NULL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001255
1256 return 0;
1257}
1258
Anirudh Ghayalf1f1e142011-10-10 17:47:45 +05301259static struct platform_driver pm8058_driver = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001260 .probe = pm8058_probe,
1261 .remove = __devexit_p(pm8058_remove),
Anirudh Ghayalf1f1e142011-10-10 17:47:45 +05301262 .driver = {
1263 .name = "pm8058-core",
1264 .owner = THIS_MODULE,
1265 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001266};
1267
1268static int __init pm8058_init(void)
1269{
Anirudh Ghayalf1f1e142011-10-10 17:47:45 +05301270 return platform_driver_register(&pm8058_driver);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001271}
Anirudh Ghayalc2019332011-11-12 06:29:10 +05301272postcore_initcall(pm8058_init);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001273
1274static void __exit pm8058_exit(void)
1275{
Anirudh Ghayalf1f1e142011-10-10 17:47:45 +05301276 platform_driver_unregister(&pm8058_driver);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001277}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001278module_exit(pm8058_exit);
1279
1280MODULE_LICENSE("GPL v2");
1281MODULE_DESCRIPTION("PMIC8058 core driver");
1282MODULE_VERSION("1.0");
1283MODULE_ALIAS("platform:pmic8058-core");