blob: ee5294bf2c6dd3c22e4ad614049e8ac3b92af05d [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 */
17#include <linux/interrupt.h>
18#include <linux/i2c.h>
David Collins0a911602011-06-15 15:03:13 -070019#include <linux/bitops.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070020#include <linux/slab.h>
21#include <linux/ratelimit.h>
22#include <linux/kthread.h>
23#include <linux/mfd/core.h>
24#include <linux/mfd/pmic8058.h>
25#include <linux/platform_device.h>
26#include <linux/ratelimit.h>
27#include <linux/slab.h>
28#include <linux/debugfs.h>
29#include <linux/irq.h>
Abhijeet Dharmapurikara3c0d942011-07-25 17:53:45 -070030#include <linux/syscore_ops.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070031
32/* PMIC8058 Revision */
33#define SSBI_REG_REV 0x002 /* PMIC4 revision */
34
35/* PMIC8058 IRQ */
36#define SSBI_REG_ADDR_IRQ_BASE 0x1BB
37
38#define SSBI_REG_ADDR_IRQ_ROOT (SSBI_REG_ADDR_IRQ_BASE + 0)
39#define SSBI_REG_ADDR_IRQ_M_STATUS1 (SSBI_REG_ADDR_IRQ_BASE + 1)
40#define SSBI_REG_ADDR_IRQ_M_STATUS2 (SSBI_REG_ADDR_IRQ_BASE + 2)
41#define SSBI_REG_ADDR_IRQ_M_STATUS3 (SSBI_REG_ADDR_IRQ_BASE + 3)
42#define SSBI_REG_ADDR_IRQ_M_STATUS4 (SSBI_REG_ADDR_IRQ_BASE + 4)
43#define SSBI_REG_ADDR_IRQ_BLK_SEL (SSBI_REG_ADDR_IRQ_BASE + 5)
44#define SSBI_REG_ADDR_IRQ_IT_STATUS (SSBI_REG_ADDR_IRQ_BASE + 6)
45#define SSBI_REG_ADDR_IRQ_CONFIG (SSBI_REG_ADDR_IRQ_BASE + 7)
46#define SSBI_REG_ADDR_IRQ_RT_STATUS (SSBI_REG_ADDR_IRQ_BASE + 8)
47
48#define PM8058_IRQF_LVL_SEL 0x01 /* level select */
49#define PM8058_IRQF_MASK_FE 0x02 /* mask falling edge */
50#define PM8058_IRQF_MASK_RE 0x04 /* mask rising edge */
51#define PM8058_IRQF_CLR 0x08 /* clear interrupt */
52#define PM8058_IRQF_BITS_MASK 0x70
53#define PM8058_IRQF_BITS_SHIFT 4
54#define PM8058_IRQF_WRITE 0x80
55
56#define PM8058_IRQF_MASK_ALL (PM8058_IRQF_MASK_FE | \
57 PM8058_IRQF_MASK_RE)
58#define PM8058_IRQF_W_C_M (PM8058_IRQF_WRITE | \
59 PM8058_IRQF_CLR | \
60 PM8058_IRQF_MASK_ALL)
61
62/* MISC register */
63#define SSBI_REG_ADDR_MISC 0x1CC
64
65/* PON CNTL 1 register */
66#define SSBI_REG_ADDR_PON_CNTL_1 0x01C
67
68#define PM8058_PON_PUP_MASK 0xF0
69
70#define PM8058_PON_WD_EN_MASK 0x08
71#define PM8058_PON_WD_EN_RESET 0x08
72#define PM8058_PON_WD_EN_PWR_OFF 0x00
73
74/* PON CNTL 4 register */
75#define SSBI_REG_ADDR_PON_CNTL_4 0x98
76#define PM8058_PON_RESET_EN_MASK 0x01
77
78/* PON CNTL 5 register */
79#define SSBI_REG_ADDR_PON_CNTL_5 0x7B
80#define PM8058_HARD_RESET_EN_MASK 0x08
81
David Collins0a911602011-06-15 15:03:13 -070082/* Regulator master enable addresses */
83#define SSBI_REG_ADDR_VREG_EN_MSM 0x018
84#define SSBI_REG_ADDR_VREG_EN_GRP_5_4 0x1C8
85
86/* Regulator control registers for shutdown/reset */
87#define SSBI_REG_ADDR_S0_CTRL 0x004
88#define SSBI_REG_ADDR_S1_CTRL 0x005
89#define SSBI_REG_ADDR_S3_CTRL 0x111
90#define SSBI_REG_ADDR_L21_CTRL 0x120
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070091#define SSBI_REG_ADDR_L22_CTRL 0x121
92
David Collins0a911602011-06-15 15:03:13 -070093#define REGULATOR_ENABLE_MASK 0x80
94#define REGULATOR_ENABLE 0x80
95#define REGULATOR_DISABLE 0x00
96#define REGULATOR_PULL_DOWN_MASK 0x40
97#define REGULATOR_PULL_DOWN_EN 0x40
98#define REGULATOR_PULL_DOWN_DIS 0x00
99
100/* Buck CTRL register */
101#define SMPS_LEGACY_VREF_SEL 0x20
102#define SMPS_LEGACY_VPROG_MASK 0x1F
103#define SMPS_ADVANCED_BAND_MASK 0xC0
104#define SMPS_ADVANCED_BAND_SHIFT 6
105#define SMPS_ADVANCED_VPROG_MASK 0x3F
106
107/* Buck TEST2 registers for shutdown/reset */
108#define SSBI_REG_ADDR_S0_TEST2 0x084
109#define SSBI_REG_ADDR_S1_TEST2 0x085
110#define SSBI_REG_ADDR_S3_TEST2 0x11A
111
112#define REGULATOR_BANK_WRITE 0x80
113#define REGULATOR_BANK_MASK 0x70
114#define REGULATOR_BANK_SHIFT 4
115#define REGULATOR_BANK_SEL(n) ((n) << REGULATOR_BANK_SHIFT)
116
117/* Buck TEST2 register bank 1 */
118#define SMPS_LEGACY_VLOW_SEL 0x01
119
120/* Buck TEST2 register bank 7 */
121#define SMPS_ADVANCED_MODE_MASK 0x02
122#define SMPS_ADVANCED_MODE 0x02
123#define SMPS_LEGACY_MODE 0x00
124
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700125/* SLEEP CNTL register */
126#define SSBI_REG_ADDR_SLEEP_CNTL 0x02B
127
128#define PM8058_SLEEP_SMPL_EN_MASK 0x04
129#define PM8058_SLEEP_SMPL_EN_RESET 0x04
130#define PM8058_SLEEP_SMPL_EN_PWR_OFF 0x00
131
132#define PM8058_SLEEP_SMPL_SEL_MASK 0x03
133#define PM8058_SLEEP_SMPL_SEL_MIN 0
134#define PM8058_SLEEP_SMPL_SEL_MAX 3
135
Willie Ruan6a3c9142011-07-14 16:52:41 -0700136/* GP_TEST1 register */
137#define SSBI_REG_ADDR_GP_TEST_1 0x07A
138
139/* IRQ */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700140#define MAX_PM_IRQ 256
141#define MAX_PM_BLOCKS (MAX_PM_IRQ / 8 + 1)
142#define MAX_PM_MASTERS (MAX_PM_BLOCKS / 8 + 1)
143
144struct pm8058_chip {
145 struct pm8058_platform_data pdata;
146
147 struct i2c_client *dev;
148
149 u8 irqs_allowed[MAX_PM_BLOCKS];
150 u8 blocks_allowed[MAX_PM_MASTERS];
151 u8 masters_allowed;
152 int pm_max_irq;
153 int pm_max_blocks;
154 int pm_max_masters;
155
156 u8 config[MAX_PM_IRQ];
157 u8 bus_unlock_config[MAX_PM_IRQ];
158 u8 wake_enable[MAX_PM_IRQ];
159 u16 count_wakeable;
160
161 u8 revision;
162
163 struct mutex pm_lock;
164};
165
166#if defined(CONFIG_DEBUG_FS)
167struct pm8058_dbg_device {
168 struct mutex dbg_mutex;
169 struct pm8058_chip *pm_chip;
170 struct dentry *dent;
171 int addr;
172};
173
174static struct pm8058_dbg_device *pmic_dbg_device;
175#endif
176
177static struct pm8058_chip *pmic_chip;
178
179/* Helper Functions */
180DEFINE_RATELIMIT_STATE(pm8058_msg_ratelimit, 60 * HZ, 10);
181
182static inline int pm8058_can_print(void)
183{
184 return __ratelimit(&pm8058_msg_ratelimit);
185}
186
187static inline int
188ssbi_write(struct i2c_client *client, u16 addr, const u8 *buf, size_t len)
189{
190 int rc;
191 struct i2c_msg msg = {
192 .addr = addr,
193 .flags = 0x0,
194 .buf = (u8 *)buf,
195 .len = len,
196 };
197
198 rc = i2c_transfer(client->adapter, &msg, 1);
199 return (rc == 1) ? 0 : rc;
200}
201
202static inline int
203ssbi_read(struct i2c_client *client, u16 addr, u8 *buf, size_t len)
204{
205 int rc;
206 struct i2c_msg msg = {
207 .addr = addr,
208 .flags = I2C_M_RD,
209 .buf = buf,
210 .len = len,
211 };
212
213 rc = i2c_transfer(client->adapter, &msg, 1);
214 return (rc == 1) ? 0 : rc;
215}
216
217static int pm8058_masked_write(u16 addr, u8 val, u8 mask)
218{
219 int rc;
220 u8 reg;
221
222 if (pmic_chip == NULL)
223 return -ENODEV;
224
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700225 rc = ssbi_read(pmic_chip->dev, addr, &reg, 1);
226 if (rc) {
227 pr_err("%s: ssbi_read(0x%03X) failed: rc=%d\n", __func__, addr,
228 rc);
229 goto done;
230 }
231
232 reg &= ~mask;
233 reg |= val & mask;
234
235 rc = ssbi_write(pmic_chip->dev, addr, &reg, 1);
236 if (rc)
237 pr_err("%s: ssbi_write(0x%03X)=0x%02X failed: rc=%d\n",
238 __func__, addr, reg, rc);
239done:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700240 return rc;
241}
242
243/* External APIs */
244int pm8058_rev(struct pm8058_chip *chip)
245{
246 if (chip == NULL)
247 return -EINVAL;
248
249 return chip->revision;
250}
251EXPORT_SYMBOL(pm8058_rev);
252
253int pm8058_irq_get_rt_status(struct pm8058_chip *chip, int irq)
254{
255 int rc;
256 u8 block, bits, bit;
257
258 if (chip == NULL || irq < chip->pdata.irq_base ||
259 irq >= chip->pdata.irq_base + MAX_PM_IRQ)
260 return -EINVAL;
261
262 irq -= chip->pdata.irq_base;
263
264 block = irq / 8;
265 bit = irq % 8;
266
267 mutex_lock(&chip->pm_lock);
268
269 rc = ssbi_write(chip->dev, SSBI_REG_ADDR_IRQ_BLK_SEL, &block, 1);
270 if (rc) {
271 pr_err("%s: FAIL ssbi_write(): rc=%d (Select Block)\n",
272 __func__, rc);
273 goto bail_out;
274 }
275
276 rc = ssbi_read(chip->dev, SSBI_REG_ADDR_IRQ_RT_STATUS, &bits, 1);
277 if (rc) {
278 pr_err("%s: FAIL ssbi_read(): rc=%d (Read RT Status)\n",
279 __func__, rc);
280 goto bail_out;
281 }
282
283 rc = (bits & (1 << bit)) ? 1 : 0;
284
285bail_out:
286 mutex_unlock(&chip->pm_lock);
287
288 return rc;
289}
290EXPORT_SYMBOL(pm8058_irq_get_rt_status);
291
292int pm8058_read(struct pm8058_chip *chip, u16 addr, u8 *values,
293 unsigned int len)
294{
295 if (chip == NULL)
296 return -EINVAL;
297
298 return ssbi_read(chip->dev, addr, values, len);
299}
300EXPORT_SYMBOL(pm8058_read);
301
302int pm8058_write(struct pm8058_chip *chip, u16 addr, u8 *values,
303 unsigned int len)
304{
305 if (chip == NULL)
306 return -EINVAL;
307
308 return ssbi_write(chip->dev, addr, values, len);
309}
310EXPORT_SYMBOL(pm8058_write);
311
312int pm8058_misc_control(struct pm8058_chip *chip, int mask, int flag)
313{
314 int rc;
315 u8 misc;
316
317 if (chip == NULL)
318 chip = pmic_chip; /* for calls from non child */
319 if (chip == NULL)
320 return -ENODEV;
321
322 mutex_lock(&chip->pm_lock);
323
324 rc = ssbi_read(chip->dev, SSBI_REG_ADDR_MISC, &misc, 1);
325 if (rc) {
326 pr_err("%s: FAIL ssbi_read(0x%x): rc=%d\n",
327 __func__, SSBI_REG_ADDR_MISC, rc);
328 goto get_out;
329 }
330
331 misc &= ~mask;
332 misc |= flag;
333
334 rc = ssbi_write(chip->dev, SSBI_REG_ADDR_MISC, &misc, 1);
335 if (rc) {
336 pr_err("%s: FAIL ssbi_write(0x%x)=0x%x: rc=%d\n",
337 __func__, SSBI_REG_ADDR_MISC, misc, rc);
338 goto get_out;
339 }
340
341get_out:
342 mutex_unlock(&chip->pm_lock);
343
344 return rc;
345}
346EXPORT_SYMBOL(pm8058_misc_control);
347
348/**
349 * pm8058_smpl_control - enables/disables SMPL detection
350 * @enable: 0 = shutdown PMIC on power loss, 1 = reset PMIC on power loss
351 *
352 * This function enables or disables the Sudden Momentary Power Loss detection
353 * module. If SMPL detection is enabled, then when a sufficiently long power
354 * loss event occurs, the PMIC will automatically reset itself. If SMPL
355 * detection is disabled, then the PMIC will shutdown when power loss occurs.
356 *
357 * RETURNS: an appropriate -ERRNO error value on error, or zero for success.
358 */
359int pm8058_smpl_control(int enable)
360{
361 return pm8058_masked_write(SSBI_REG_ADDR_SLEEP_CNTL,
362 (enable ? PM8058_SLEEP_SMPL_EN_RESET
363 : PM8058_SLEEP_SMPL_EN_PWR_OFF),
364 PM8058_SLEEP_SMPL_EN_MASK);
365}
366EXPORT_SYMBOL(pm8058_smpl_control);
367
368/**
369 * pm8058_smpl_set_delay - sets the SMPL detection time delay
370 * @delay: enum value corresponding to delay time
371 *
372 * This function sets the time delay of the SMPL detection module. If power
373 * is reapplied within this interval, then the PMIC reset automatically. The
374 * SMPL detection module must be enabled for this delay time to take effect.
375 *
376 * RETURNS: an appropriate -ERRNO error value on error, or zero for success.
377 */
378int pm8058_smpl_set_delay(enum pm8058_smpl_delay delay)
379{
380 if (delay < PM8058_SLEEP_SMPL_SEL_MIN
381 || delay > PM8058_SLEEP_SMPL_SEL_MAX) {
382 pr_err("%s: invalid delay specified: %d\n", __func__, delay);
383 return -EINVAL;
384 }
385
386 return pm8058_masked_write(SSBI_REG_ADDR_SLEEP_CNTL, delay,
387 PM8058_SLEEP_SMPL_SEL_MASK);
388}
389EXPORT_SYMBOL(pm8058_smpl_set_delay);
390
391/**
392 * pm8058_watchdog_reset_control - enables/disables watchdog reset detection
393 * @enable: 0 = shutdown when PS_HOLD goes low, 1 = reset when PS_HOLD goes low
394 *
395 * This function enables or disables the PMIC watchdog reset detection feature.
396 * If watchdog reset detection is enabled, then the PMIC will reset itself
397 * when PS_HOLD goes low. If it is not enabled, then the PMIC will shutdown
398 * when PS_HOLD goes low.
399 *
400 * RETURNS: an appropriate -ERRNO error value on error, or zero for success.
401 */
402int pm8058_watchdog_reset_control(int enable)
403{
404 return pm8058_masked_write(SSBI_REG_ADDR_PON_CNTL_1,
405 (enable ? PM8058_PON_WD_EN_RESET
406 : PM8058_PON_WD_EN_PWR_OFF),
407 PM8058_PON_WD_EN_MASK);
408}
409EXPORT_SYMBOL(pm8058_watchdog_reset_control);
410
David Collins0a911602011-06-15 15:03:13 -0700411/*
412 * Set an SMPS regulator to be disabled in its CTRL register, but enabled
413 * in the master enable register. Also set it's pull down enable bit.
414 * Take care to make sure that the output voltage doesn't change if switching
415 * from advanced mode to legacy mode.
416 */
417static int disable_smps_locally_set_pull_down(u16 ctrl_addr, u16 test2_addr,
418 u16 master_enable_addr, u8 master_enable_bit)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700419{
David Collins0a911602011-06-15 15:03:13 -0700420 int rc = 0;
421 u8 vref_sel, vlow_sel, band, vprog, bank, reg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700422
423 if (pmic_chip == NULL)
424 return -ENODEV;
425
David Collins0a911602011-06-15 15:03:13 -0700426 bank = REGULATOR_BANK_SEL(7);
427 rc = ssbi_write(pmic_chip->dev, test2_addr, &bank, 1);
428 if (rc) {
429 pr_err("%s: FAIL ssbi_write(0x%03X): rc=%d\n", __func__,
430 test2_addr, rc);
431 goto done;
432 }
433
434 rc = ssbi_read(pmic_chip->dev, test2_addr, &reg, 1);
435 if (rc) {
436 pr_err("%s: FAIL pm8058_read(0x%03X): rc=%d\n",
437 __func__, test2_addr, rc);
438 goto done;
439 }
440
441 /* Check if in advanced mode. */
442 if ((reg & SMPS_ADVANCED_MODE_MASK) == SMPS_ADVANCED_MODE) {
443 /* Determine current output voltage. */
444 rc = ssbi_read(pmic_chip->dev, ctrl_addr, &reg, 1);
445 if (rc) {
446 pr_err("%s: FAIL pm8058_read(0x%03X): rc=%d\n",
447 __func__, ctrl_addr, rc);
448 goto done;
449 }
450
451 band = (reg & SMPS_ADVANCED_BAND_MASK)
452 >> SMPS_ADVANCED_BAND_SHIFT;
453 switch (band) {
454 case 3:
455 vref_sel = 0;
456 vlow_sel = 0;
457 break;
458 case 2:
459 vref_sel = SMPS_LEGACY_VREF_SEL;
460 vlow_sel = 0;
461 break;
462 case 1:
463 vref_sel = SMPS_LEGACY_VREF_SEL;
464 vlow_sel = SMPS_LEGACY_VLOW_SEL;
465 break;
466 default:
467 pr_err("%s: regulator already disabled\n", __func__);
468 return -EPERM;
469 }
470 vprog = (reg & SMPS_ADVANCED_VPROG_MASK);
471 /* Round up if fine step is in use. */
472 vprog = (vprog + 1) >> 1;
473 if (vprog > SMPS_LEGACY_VPROG_MASK)
474 vprog = SMPS_LEGACY_VPROG_MASK;
475
476 /* Set VLOW_SEL bit. */
477 bank = REGULATOR_BANK_SEL(1);
478 rc = ssbi_write(pmic_chip->dev, test2_addr, &bank, 1);
479 if (rc) {
480 pr_err("%s: FAIL ssbi_write(0x%03X): rc=%d\n",
481 __func__, test2_addr, rc);
482 goto done;
483 }
484 rc = pm8058_masked_write(test2_addr,
485 REGULATOR_BANK_WRITE | REGULATOR_BANK_SEL(1)
486 | vlow_sel,
487 REGULATOR_BANK_WRITE | REGULATOR_BANK_MASK
488 | SMPS_LEGACY_VLOW_SEL);
489 if (rc)
490 goto done;
491
492 /* Switch to legacy mode */
493 bank = REGULATOR_BANK_SEL(7);
494 rc = ssbi_write(pmic_chip->dev, test2_addr, &bank, 1);
495 if (rc) {
496 pr_err("%s: FAIL ssbi_write(0x%03X): rc=%d\n", __func__,
497 test2_addr, rc);
498 goto done;
499 }
500 rc = pm8058_masked_write(test2_addr,
501 REGULATOR_BANK_WRITE | REGULATOR_BANK_SEL(7)
502 | SMPS_LEGACY_MODE,
503 REGULATOR_BANK_WRITE | REGULATOR_BANK_MASK
504 | SMPS_ADVANCED_MODE_MASK);
505 if (rc)
506 goto done;
507
508 /* Enable locally, enable pull down, keep voltage the same. */
509 rc = pm8058_masked_write(ctrl_addr,
510 REGULATOR_ENABLE | REGULATOR_PULL_DOWN_EN
511 | vref_sel | vprog,
512 REGULATOR_ENABLE_MASK | REGULATOR_PULL_DOWN_MASK
513 | SMPS_LEGACY_VREF_SEL | SMPS_LEGACY_VPROG_MASK);
514 if (rc)
515 goto done;
516 }
517
518 /* Enable in master control register. */
519 rc = pm8058_masked_write(master_enable_addr, master_enable_bit,
520 master_enable_bit);
521 if (rc)
522 goto done;
523
524 /* Disable locally and enable pull down. */
525 rc = pm8058_masked_write(ctrl_addr,
526 REGULATOR_DISABLE | REGULATOR_PULL_DOWN_EN,
527 REGULATOR_ENABLE_MASK | REGULATOR_PULL_DOWN_MASK);
528
529done:
530 return rc;
531}
532
533static int disable_ldo_locally_set_pull_down(u16 ctrl_addr,
534 u16 master_enable_addr, u8 master_enable_bit)
535{
536 int rc;
537
538 /* Enable LDO in master control register. */
539 rc = pm8058_masked_write(master_enable_addr, master_enable_bit,
540 master_enable_bit);
541 if (rc)
542 goto done;
543
544 /* Disable LDO in CTRL register and set pull down */
545 rc = pm8058_masked_write(ctrl_addr,
546 REGULATOR_DISABLE | REGULATOR_PULL_DOWN_EN,
547 REGULATOR_ENABLE_MASK | REGULATOR_PULL_DOWN_MASK);
548
549done:
550 return rc;
551}
552
553int pm8058_reset_pwr_off(int reset)
554{
555 int rc;
556 u8 pon, ctrl, smpl;
557
558 if (pmic_chip == NULL)
559 return -ENODEV;
560
561 /* When shutting down, enable active pulldowns on important rails. */
562 if (!reset) {
563 /* Disable SMPS's 0,1,3 locally and set pulldown enable bits. */
564 disable_smps_locally_set_pull_down(SSBI_REG_ADDR_S0_CTRL,
565 SSBI_REG_ADDR_S0_TEST2, SSBI_REG_ADDR_VREG_EN_MSM, BIT(7));
566 disable_smps_locally_set_pull_down(SSBI_REG_ADDR_S1_CTRL,
567 SSBI_REG_ADDR_S1_TEST2, SSBI_REG_ADDR_VREG_EN_MSM, BIT(6));
568 disable_smps_locally_set_pull_down(SSBI_REG_ADDR_S3_CTRL,
569 SSBI_REG_ADDR_S3_TEST2, SSBI_REG_ADDR_VREG_EN_GRP_5_4,
570 BIT(7) | BIT(4));
571 /* Disable LDO 21 locally and set pulldown enable bit. */
572 disable_ldo_locally_set_pull_down(SSBI_REG_ADDR_L21_CTRL,
573 SSBI_REG_ADDR_VREG_EN_GRP_5_4, BIT(1));
574 }
575
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700576 /* Set regulator L22 to 1.225V in high power mode. */
577 rc = ssbi_read(pmic_chip->dev, SSBI_REG_ADDR_L22_CTRL, &ctrl, 1);
578 if (rc) {
579 pr_err("%s: FAIL ssbi_read(0x%x): rc=%d\n", __func__,
580 SSBI_REG_ADDR_L22_CTRL, rc);
581 goto get_out3;
582 }
583 /* Leave pull-down state intact. */
584 ctrl &= 0x40;
585 ctrl |= 0x93;
586 rc = ssbi_write(pmic_chip->dev, SSBI_REG_ADDR_L22_CTRL, &ctrl, 1);
587 if (rc)
588 pr_err("%s: FAIL ssbi_write(0x%x)=0x%x: rc=%d\n", __func__,
589 SSBI_REG_ADDR_L22_CTRL, ctrl, rc);
590
591get_out3:
592 if (!reset) {
593 /* Only modify the SLEEP_CNTL reg if shutdown is desired. */
594 rc = ssbi_read(pmic_chip->dev, SSBI_REG_ADDR_SLEEP_CNTL,
595 &smpl, 1);
596 if (rc) {
597 pr_err("%s: FAIL ssbi_read(0x%x): rc=%d\n",
598 __func__, SSBI_REG_ADDR_SLEEP_CNTL, rc);
599 goto get_out2;
600 }
601
602 smpl &= ~PM8058_SLEEP_SMPL_EN_MASK;
603 smpl |= PM8058_SLEEP_SMPL_EN_PWR_OFF;
604
605 rc = ssbi_write(pmic_chip->dev, SSBI_REG_ADDR_SLEEP_CNTL,
606 &smpl, 1);
607 if (rc)
608 pr_err("%s: FAIL ssbi_write(0x%x)=0x%x: rc=%d\n",
609 __func__, SSBI_REG_ADDR_SLEEP_CNTL, smpl, rc);
610 }
611
612get_out2:
613 rc = ssbi_read(pmic_chip->dev, SSBI_REG_ADDR_PON_CNTL_1, &pon, 1);
614 if (rc) {
615 pr_err("%s: FAIL ssbi_read(0x%x): rc=%d\n",
616 __func__, SSBI_REG_ADDR_PON_CNTL_1, rc);
617 goto get_out;
618 }
619
620 pon &= ~PM8058_PON_WD_EN_MASK;
621 pon |= reset ? PM8058_PON_WD_EN_RESET : PM8058_PON_WD_EN_PWR_OFF;
622
623 /* Enable all pullups */
624 pon |= PM8058_PON_PUP_MASK;
625
626 rc = ssbi_write(pmic_chip->dev, SSBI_REG_ADDR_PON_CNTL_1, &pon, 1);
627 if (rc) {
628 pr_err("%s: FAIL ssbi_write(0x%x)=0x%x: rc=%d\n",
629 __func__, SSBI_REG_ADDR_PON_CNTL_1, pon, rc);
630 goto get_out;
631 }
632
633get_out:
634 return rc;
635}
636EXPORT_SYMBOL(pm8058_reset_pwr_off);
637
Willie Ruan6a3c9142011-07-14 16:52:41 -0700638/**
639 * pm8058_stay_on - enables stay_on feature
640 *
641 * PMIC stay-on feature allows PMIC to ignore MSM PS_HOLD=low
642 * signal so that some special functions like debugging could be
643 * performed.
644 *
645 * This feature should not be used in any product release.
646 *
647 * RETURNS: an appropriate -ERRNO error value on error, or zero for success.
648 */
649int pm8058_stay_on(void)
650{
651 u8 ctrl = 0x92;
652 int rc;
653
654 rc = ssbi_write(pmic_chip->dev, SSBI_REG_ADDR_GP_TEST_1, &ctrl, 1);
655 pr_info("%s: set stay-on: rc = %d\n", __func__, rc);
656 return rc;
657}
658EXPORT_SYMBOL(pm8058_stay_on);
659
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700660/*
661 power on hard reset configuration
662 config = DISABLE_HARD_RESET to disable hard reset
663 = SHUTDOWN_ON_HARD_RESET to turn off the system on hard reset
664 = RESTART_ON_HARD_RESET to restart the system on hard reset
665 */
666int pm8058_hard_reset_config(enum pon_config config)
667{
668 int rc, ret;
669 u8 pon, pon_5;
670
671 if (config >= MAX_PON_CONFIG)
672 return -EINVAL;
673
674 if (pmic_chip == NULL)
675 return -ENODEV;
676
677 mutex_lock(&pmic_chip->pm_lock);
678
679 rc = ssbi_read(pmic_chip->dev, SSBI_REG_ADDR_PON_CNTL_5, &pon, 1);
680 if (rc) {
681 pr_err("%s: FAIL ssbi_read(0x%x): rc=%d\n",
682 __func__, SSBI_REG_ADDR_PON_CNTL_5, rc);
683 mutex_unlock(&pmic_chip->pm_lock);
684 return rc;
685 }
686
687 pon_5 = pon;
688 (config != DISABLE_HARD_RESET) ? (pon |= PM8058_HARD_RESET_EN_MASK) :
689 (pon &= ~PM8058_HARD_RESET_EN_MASK);
690
691 rc = ssbi_write(pmic_chip->dev, SSBI_REG_ADDR_PON_CNTL_5, &pon, 1);
692 if (rc) {
693 pr_err("%s: FAIL ssbi_write(0x%x)=0x%x: rc=%d\n",
694 __func__, SSBI_REG_ADDR_PON_CNTL_5, pon, rc);
695 mutex_unlock(&pmic_chip->pm_lock);
696 return rc;
697 }
698
699 if (config == DISABLE_HARD_RESET) {
700 mutex_unlock(&pmic_chip->pm_lock);
701 return 0;
702 }
703
704 rc = ssbi_read(pmic_chip->dev, SSBI_REG_ADDR_PON_CNTL_4, &pon, 1);
705 if (rc) {
706 pr_err("%s: FAIL ssbi_read(0x%x): rc=%d\n",
707 __func__, SSBI_REG_ADDR_PON_CNTL_4, rc);
708 goto err_restore_pon_5;
709 }
710
711 (config == RESTART_ON_HARD_RESET) ? (pon |= PM8058_PON_RESET_EN_MASK) :
712 (pon &= ~PM8058_PON_RESET_EN_MASK);
713
714 rc = ssbi_write(pmic_chip->dev, SSBI_REG_ADDR_PON_CNTL_4, &pon, 1);
715 if (rc) {
716 pr_err("%s: FAIL ssbi_write(0x%x)=0x%x: rc=%d\n",
717 __func__, SSBI_REG_ADDR_PON_CNTL_4, pon, rc);
718 goto err_restore_pon_5;
719 }
720 mutex_unlock(&pmic_chip->pm_lock);
721 return 0;
722
723err_restore_pon_5:
724 ret = ssbi_write(pmic_chip->dev, SSBI_REG_ADDR_PON_CNTL_5, &pon_5, 1);
725 if (ret)
726 pr_err("%s: FAIL ssbi_write(0x%x)=0x%x: rc=%d\n",
727 __func__, SSBI_REG_ADDR_PON_CNTL_5, pon, ret);
728 mutex_unlock(&pmic_chip->pm_lock);
729 return rc;
730}
731EXPORT_SYMBOL(pm8058_hard_reset_config);
732
733/* Internal functions */
734static inline int
735pm8058_config_irq(struct pm8058_chip *chip, u8 *bp, u8 *cp)
736{
737 int rc;
738
739 rc = ssbi_write(chip->dev, SSBI_REG_ADDR_IRQ_BLK_SEL, bp, 1);
740 if (rc) {
741 pr_err("%s: ssbi_write: rc=%d (Select block)\n",
742 __func__, rc);
743 goto bail_out;
744 }
745
746 rc = ssbi_write(chip->dev, SSBI_REG_ADDR_IRQ_CONFIG, cp, 1);
747 if (rc)
748 pr_err("%s: ssbi_write: rc=%d (Configure IRQ)\n",
749 __func__, rc);
750
751bail_out:
752 return rc;
753}
754
755static void pm8058_irq_mask(struct irq_data *data)
756{
757 int master, irq_bit;
758 struct pm8058_chip *chip = irq_data_get_irq_chip_data(data);
759 u8 block, config;
760 unsigned int irq = data->irq;
761
762 irq -= chip->pdata.irq_base;
763 block = irq / 8;
764 master = block / 8;
765 irq_bit = irq % 8;
766
767 chip->irqs_allowed[block] &= ~(1 << irq_bit);
768 if (!chip->irqs_allowed[block]) {
769 chip->blocks_allowed[master] &= ~(1 << (block % 8));
770
771 if (!chip->blocks_allowed[master])
772 chip->masters_allowed &= ~(1 << master);
773 }
774
775 config = PM8058_IRQF_WRITE | chip->config[irq] |
776 PM8058_IRQF_MASK_FE | PM8058_IRQF_MASK_RE;
777 chip->bus_unlock_config[irq] = config;
778}
779
780static void pm8058_irq_unmask(struct irq_data *data)
781{
782 int master, irq_bit;
783 struct pm8058_chip *chip = irq_data_get_irq_chip_data(data);
784 u8 block, config, old_irqs_allowed, old_blocks_allowed;
785 unsigned int irq = data->irq;
786
787 irq -= chip->pdata.irq_base;
788 block = irq / 8;
789 master = block / 8;
790 irq_bit = irq % 8;
791
792 old_irqs_allowed = chip->irqs_allowed[block];
793 chip->irqs_allowed[block] |= 1 << irq_bit;
794 if (!old_irqs_allowed) {
795 master = block / 8;
796
797 old_blocks_allowed = chip->blocks_allowed[master];
798 chip->blocks_allowed[master] |= 1 << (block % 8);
799
800 if (!old_blocks_allowed)
801 chip->masters_allowed |= 1 << master;
802 }
803
804 config = PM8058_IRQF_WRITE | chip->config[irq];
805 chip->bus_unlock_config[irq] = config;
806}
807
808static void pm8058_irq_ack(struct irq_data *data)
809{
810 struct pm8058_chip *chip = irq_data_get_irq_chip_data(data);
811 u8 block, config;
812 unsigned int irq = data->irq;
813
814 irq -= chip->pdata.irq_base;
815 block = irq / 8;
816
817 config = PM8058_IRQF_WRITE | chip->config[irq] | PM8058_IRQF_CLR;
818 /* Keep the mask */
819 if (!(chip->irqs_allowed[block] & (1 << (irq % 8))))
820 config |= PM8058_IRQF_MASK_FE | PM8058_IRQF_MASK_RE;
821 chip->bus_unlock_config[irq] = config;
822}
823
824static int pm8058_irq_set_type(struct irq_data *data, unsigned int flow_type)
825{
826 int master, irq_bit;
827 struct pm8058_chip *chip = irq_data_get_irq_chip_data(data);
828 u8 block, config;
829 unsigned int irq = data->irq;
830
831 irq -= chip->pdata.irq_base;
832 if (irq > chip->pm_max_irq) {
833 chip->pm_max_irq = irq;
834 chip->pm_max_blocks =
835 chip->pm_max_irq / 8 + 1;
836 chip->pm_max_masters =
837 chip->pm_max_blocks / 8 + 1;
838 }
839 block = irq / 8;
840 master = block / 8;
841 irq_bit = irq % 8;
842
843 chip->config[irq] = (irq_bit << PM8058_IRQF_BITS_SHIFT) |
844 PM8058_IRQF_MASK_RE | PM8058_IRQF_MASK_FE;
845 if (flow_type & (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING)) {
846 if (flow_type & IRQF_TRIGGER_RISING)
847 chip->config[irq] &= ~PM8058_IRQF_MASK_RE;
848 if (flow_type & IRQF_TRIGGER_FALLING)
849 chip->config[irq] &= ~PM8058_IRQF_MASK_FE;
850 } else {
851 chip->config[irq] |= PM8058_IRQF_LVL_SEL;
852
853 if (flow_type & IRQF_TRIGGER_HIGH)
854 chip->config[irq] &= ~PM8058_IRQF_MASK_RE;
855 else
856 chip->config[irq] &= ~PM8058_IRQF_MASK_FE;
857 }
858
859 config = PM8058_IRQF_WRITE | chip->config[irq] | PM8058_IRQF_CLR;
860 chip->bus_unlock_config[irq] = config;
861 return 0;
862}
863
864static int pm8058_irq_set_wake(struct irq_data *data, unsigned int on)
865{
866 struct pm8058_chip *chip = irq_data_get_irq_chip_data(data);
867 unsigned int irq = data->irq;
868
869 irq -= chip->pdata.irq_base;
870 if (on) {
871 if (!chip->wake_enable[irq]) {
872 chip->wake_enable[irq] = 1;
873 chip->count_wakeable++;
874 }
875 } else {
876 if (chip->wake_enable[irq]) {
877 chip->wake_enable[irq] = 0;
878 chip->count_wakeable--;
879 }
880 }
881
882 return 0;
883}
884
885static void pm8058_irq_bus_lock(struct irq_data *data)
886{
887 u8 block;
888 struct pm8058_chip *chip = irq_data_get_irq_chip_data(data);
889 unsigned int irq = data->irq;
890
891 irq -= chip->pdata.irq_base;
892 block = irq / 8;
893 chip->bus_unlock_config[irq] = 0;
894
895 mutex_lock(&chip->pm_lock);
896}
897
898static void pm8058_irq_bus_sync_unlock(struct irq_data *data)
899{
900 u8 block, config;
901 struct pm8058_chip *chip = irq_data_get_irq_chip_data(data);
902 unsigned int irq = data->irq;
903
904 irq -= chip->pdata.irq_base;
905 block = irq / 8;
906 config = chip->bus_unlock_config[irq];
907 /* dont waste cpu cycles if we dont have data to write */
908 if (config)
909 pm8058_config_irq(chip, &block, &config);
910 mutex_unlock(&chip->pm_lock);
911}
912
913static inline int
914pm8058_read_root(struct pm8058_chip *chip, u8 *rp)
915{
916 int rc;
917
918 rc = ssbi_read(chip->dev, SSBI_REG_ADDR_IRQ_ROOT, rp, 1);
919 if (rc) {
920 pr_err("%s: FAIL ssbi_read(): rc=%d (Read Root)\n",
921 __func__, rc);
922 *rp = 0;
923 }
924
925 return rc;
926}
927
928static inline int
929pm8058_read_master(struct pm8058_chip *chip, u8 m, u8 *bp)
930{
931 int rc;
932
933 rc = ssbi_read(chip->dev, SSBI_REG_ADDR_IRQ_M_STATUS1 + m, bp, 1);
934 if (rc) {
935 pr_err("%s: FAIL ssbi_read(): rc=%d (Read Master)\n",
936 __func__, rc);
937 *bp = 0;
938 }
939
940 return rc;
941}
942
943static inline int
944pm8058_read_block(struct pm8058_chip *chip, u8 *bp, u8 *ip)
945{
946 int rc;
947
948 rc = ssbi_write(chip->dev, SSBI_REG_ADDR_IRQ_BLK_SEL, bp, 1);
949 if (rc) {
950 pr_err("%s: FAIL ssbi_write(): rc=%d (Select Block)\n",
951 __func__, rc);
952 *bp = 0;
953 goto bail_out;
954 }
955
956 rc = ssbi_read(chip->dev, SSBI_REG_ADDR_IRQ_IT_STATUS, ip, 1);
957 if (rc)
958 pr_err("%s: FAIL ssbi_read(): rc=%d (Read Status)\n",
959 __func__, rc);
960
961bail_out:
962 return rc;
963}
964
965static irqreturn_t pm8058_isr_thread(int irq_requested, void *data)
966{
967 struct pm8058_chip *chip = data;
968 int i, j, k;
969 u8 root, block, config, bits;
970 u8 blocks[MAX_PM_MASTERS];
971 int masters = 0, irq, handled = 0, spurious = 0;
972 u16 irqs_to_handle[MAX_PM_IRQ];
973
974 mutex_lock(&chip->pm_lock);
975
976 /* Read root for masters */
977 if (pm8058_read_root(chip, &root))
978 goto bail_out;
979
980 masters = root >> 1;
981
982 if (!(masters & chip->masters_allowed) ||
983 (masters & ~chip->masters_allowed)) {
984 spurious = 1000000;
985 }
986
987 /* Read allowed masters for blocks. */
988 for (i = 0; i < chip->pm_max_masters; i++) {
989 if (masters & (1 << i)) {
990 if (pm8058_read_master(chip, i, &blocks[i]))
991 goto bail_out;
992
993 if (!blocks[i]) {
994 if (pm8058_can_print())
995 pr_err("%s: Spurious master: %d "
996 "(blocks=0)", __func__, i);
997 spurious += 10000;
998 }
999 } else
1000 blocks[i] = 0;
1001 }
1002
1003 /* Select block, read status and call isr */
1004 for (i = 0; i < chip->pm_max_masters; i++) {
1005 if (!blocks[i])
1006 continue;
1007
1008 for (j = 0; j < 8; j++) {
1009 if (!(blocks[i] & (1 << j)))
1010 continue;
1011
1012 block = i * 8 + j; /* block # */
1013 if (pm8058_read_block(chip, &block, &bits))
1014 goto bail_out;
1015
1016 if (!bits) {
1017 if (pm8058_can_print())
1018 pr_err("%s: Spurious block: "
1019 "[master, block]=[%d, %d] "
1020 "(bits=0)\n", __func__, i, j);
1021 spurious += 100;
1022 continue;
1023 }
1024
1025 /* Check IRQ bits */
1026 for (k = 0; k < 8; k++) {
1027 if (!(bits & (1 << k)))
1028 continue;
1029
1030 /* Check spurious interrupts */
1031 if (((1 << i) & chip->masters_allowed) &&
1032 (blocks[i] & chip->blocks_allowed[i]) &&
1033 (bits & chip->irqs_allowed[block])) {
1034
1035 /* Found one */
1036 irq = block * 8 + k;
1037 irqs_to_handle[handled] = irq +
1038 chip->pdata.irq_base;
1039 handled++;
1040 } else {
1041 /* Clear and mask wrong one */
1042 config = PM8058_IRQF_W_C_M |
1043 (k << PM8058_IRQF_BITS_SHIFT);
1044
1045 pm8058_config_irq(chip,
1046 &block, &config);
1047
1048 if (pm8058_can_print())
1049 pr_err("%s: Spurious IRQ: "
1050 "[master, block, bit]="
1051 "[%d, %d (%d), %d]\n",
1052 __func__,
1053 i, j, block, k);
1054 spurious++;
1055 }
1056 }
1057 }
1058
1059 }
1060
1061bail_out:
1062
1063 mutex_unlock(&chip->pm_lock);
1064
1065 for (i = 0; i < handled; i++)
1066 handle_nested_irq(irqs_to_handle[i]);
1067
1068 for (i = 0; i < handled; i++) {
1069 irqs_to_handle[i] -= chip->pdata.irq_base;
1070 block = irqs_to_handle[i] / 8 ;
1071 config = PM8058_IRQF_WRITE | chip->config[irqs_to_handle[i]]
1072 | PM8058_IRQF_CLR;
1073 pm8058_config_irq(chip, &block, &config);
1074 }
1075
1076 if (spurious) {
1077 if (!pm8058_can_print())
1078 return IRQ_HANDLED;
1079
1080 pr_err("%s: spurious = %d (handled = %d)\n",
1081 __func__, spurious, handled);
1082 pr_err(" root = 0x%x (masters_allowed<<1 = 0x%x)\n",
1083 root, chip->masters_allowed << 1);
1084 for (i = 0; i < chip->pm_max_masters; i++) {
1085 if (masters & (1 << i))
1086 pr_err(" blocks[%d]=0x%x, "
1087 "allowed[%d]=0x%x\n",
1088 i, blocks[i],
1089 i, chip->blocks_allowed[i]);
1090 }
1091 }
1092
1093 return IRQ_HANDLED;
1094}
1095
1096#if defined(CONFIG_DEBUG_FS)
1097
1098static int check_addr(int addr, const char *func_name)
1099{
1100 if (addr < 0 || addr > 0x3FF) {
1101 pr_err("%s: PMIC 8058 register address is invalid: %d\n",
1102 func_name, addr);
1103 return -EINVAL;
1104 }
1105 return 0;
1106}
1107
1108static int data_set(void *data, u64 val)
1109{
1110 struct pm8058_dbg_device *dbgdev = data;
1111 u8 reg = val;
1112 int rc;
1113
1114 mutex_lock(&dbgdev->dbg_mutex);
1115
1116 rc = check_addr(dbgdev->addr, __func__);
1117 if (rc)
1118 goto done;
1119
1120 rc = pm8058_write(dbgdev->pm_chip, dbgdev->addr, &reg, 1);
1121
1122 if (rc)
1123 pr_err("%s: FAIL pm8058_write(0x%03X)=0x%02X: rc=%d\n",
1124 __func__, dbgdev->addr, reg, rc);
1125done:
1126 mutex_unlock(&dbgdev->dbg_mutex);
1127 return rc;
1128}
1129
1130static int data_get(void *data, u64 *val)
1131{
1132 struct pm8058_dbg_device *dbgdev = data;
1133 int rc;
1134 u8 reg;
1135
1136 mutex_lock(&dbgdev->dbg_mutex);
1137
1138 rc = check_addr(dbgdev->addr, __func__);
1139 if (rc)
1140 goto done;
1141
1142 rc = pm8058_read(dbgdev->pm_chip, dbgdev->addr, &reg, 1);
1143
1144 if (rc) {
1145 pr_err("%s: FAIL pm8058_read(0x%03X)=0x%02X: rc=%d\n",
1146 __func__, dbgdev->addr, reg, rc);
1147 goto done;
1148 }
1149
1150 *val = reg;
1151done:
1152 mutex_unlock(&dbgdev->dbg_mutex);
1153 return rc;
1154}
1155
1156DEFINE_SIMPLE_ATTRIBUTE(dbg_data_fops, data_get, data_set, "0x%02llX\n");
1157
1158static int addr_set(void *data, u64 val)
1159{
1160 struct pm8058_dbg_device *dbgdev = data;
1161 int rc;
1162
1163 rc = check_addr(val, __func__);
1164 if (rc)
1165 return rc;
1166
1167 mutex_lock(&dbgdev->dbg_mutex);
1168 dbgdev->addr = val;
1169 mutex_unlock(&dbgdev->dbg_mutex);
1170
1171 return 0;
1172}
1173
1174static int addr_get(void *data, u64 *val)
1175{
1176 struct pm8058_dbg_device *dbgdev = data;
1177 int rc;
1178
1179 mutex_lock(&dbgdev->dbg_mutex);
1180
1181 rc = check_addr(dbgdev->addr, __func__);
1182 if (rc) {
1183 mutex_unlock(&dbgdev->dbg_mutex);
1184 return rc;
1185 }
1186 *val = dbgdev->addr;
1187
1188 mutex_unlock(&dbgdev->dbg_mutex);
1189
1190 return 0;
1191}
1192
1193DEFINE_SIMPLE_ATTRIBUTE(dbg_addr_fops, addr_get, addr_set, "0x%03llX\n");
1194
1195static int __devinit pmic8058_dbg_probe(struct pm8058_chip *chip)
1196{
1197 struct pm8058_dbg_device *dbgdev;
1198 struct dentry *dent;
1199 struct dentry *temp;
1200
1201 if (chip == NULL) {
1202 pr_err("%s: no parent data passed in.\n", __func__);
1203 return -EINVAL;
1204 }
1205
1206 dbgdev = kzalloc(sizeof *dbgdev, GFP_KERNEL);
1207 if (dbgdev == NULL) {
1208 pr_err("%s: kzalloc() failed.\n", __func__);
1209 return -ENOMEM;
1210 }
1211
1212 mutex_init(&dbgdev->dbg_mutex);
1213
1214 dbgdev->pm_chip = chip;
1215 dbgdev->addr = -1;
1216
1217 dent = debugfs_create_dir("pm8058-dbg", NULL);
1218 if (dent == NULL || IS_ERR(dent)) {
1219 pr_err("%s: ERR debugfs_create_dir: dent=0x%X\n",
1220 __func__, (unsigned)dent);
1221 return -ENOMEM;
1222 }
1223
1224 temp = debugfs_create_file("addr", S_IRUSR | S_IWUSR, dent,
1225 dbgdev, &dbg_addr_fops);
1226 if (temp == NULL || IS_ERR(temp)) {
1227 pr_err("%s: ERR debugfs_create_file: dent=0x%X\n",
1228 __func__, (unsigned)temp);
1229 goto debug_error;
1230 }
1231
1232 temp = debugfs_create_file("data", S_IRUSR | S_IWUSR, dent,
1233 dbgdev, &dbg_data_fops);
1234 if (temp == NULL || IS_ERR(temp)) {
1235 pr_err("%s: ERR debugfs_create_file: dent=0x%X\n",
1236 __func__, (unsigned)temp);
1237 goto debug_error;
1238 }
1239
1240 dbgdev->dent = dent;
1241
1242 pmic_dbg_device = dbgdev;
1243
1244 return 0;
1245
1246debug_error:
1247 debugfs_remove_recursive(dent);
1248 return -ENOMEM;
1249}
1250
1251static int __devexit pmic8058_dbg_remove(void)
1252{
1253 if (pmic_dbg_device) {
1254 debugfs_remove_recursive(pmic_dbg_device->dent);
1255 kfree(pmic_dbg_device);
1256 }
1257 return 0;
1258}
1259
1260#else
1261
1262static int __devinit pmic8058_dbg_probe(struct pm8058_chip *chip)
1263{
1264 return 0;
1265}
1266
1267static int __devexit pmic8058_dbg_remove(void)
1268{
1269 return 0;
1270}
1271
1272#endif
1273
1274static struct irq_chip pm8058_irq_chip = {
1275 .name = "pm8058",
1276 .irq_ack = pm8058_irq_ack,
1277 .irq_mask = pm8058_irq_mask,
1278 .irq_unmask = pm8058_irq_unmask,
1279 .irq_set_type = pm8058_irq_set_type,
1280 .irq_set_wake = pm8058_irq_set_wake,
1281 .irq_bus_lock = pm8058_irq_bus_lock,
1282 .irq_bus_sync_unlock = pm8058_irq_bus_sync_unlock,
1283};
1284
Abhijeet Dharmapurikara3c0d942011-07-25 17:53:45 -07001285static int pm8058_suspend(void)
1286{
1287 struct pm8058_chip *chip = pmic_chip;
1288 struct irq_data *data;
1289 int i;
1290
1291 for (i = 0; i < MAX_PM_IRQ; i++) {
1292 if (chip->config[i] && !chip->wake_enable[i]) {
1293 if (!((chip->config[i] & PM8058_IRQF_MASK_ALL)
1294 == PM8058_IRQF_MASK_ALL)) {
1295 data = irq_get_irq_data(i +
1296 chip->pdata.irq_base);
1297 pm8058_irq_bus_lock(data);
1298 pm8058_irq_mask(data);
1299 pm8058_irq_bus_sync_unlock(data);
1300 }
1301 }
1302 }
1303
1304 if (!chip->count_wakeable)
1305 disable_irq(chip->dev->irq);
1306
1307 return 0;
1308}
1309
1310extern int msm_show_resume_irq_mask;
1311
1312static void pm8058_show_resume_irq(void)
1313{
1314 u8 block, bits;
1315 int i;
1316 struct pm8058_chip *chip = pmic_chip;
1317
1318 if (!msm_show_resume_irq_mask)
1319 return;
1320
1321 for (i = 0; i < MAX_PM_IRQ; i++) {
1322 if (chip->wake_enable[i]) {
1323 block = i / 8;
1324 if (!pm8058_read_block(chip, &block, &bits)) {
1325 if (bits & (1 << (i & 0x7)))
1326 pr_warning("%s:%d triggered\n",
1327 __func__, i + chip->pdata.irq_base);
1328 }
1329 }
1330 }
1331}
1332
1333static void pm8058_resume(void)
1334{
1335 struct pm8058_chip *chip = pmic_chip;
1336 struct irq_data *data;
1337 int i;
1338
1339 pm8058_show_resume_irq();
1340
1341 for (i = 0; i < MAX_PM_IRQ; i++) {
1342 if (chip->config[i] && !chip->wake_enable[i]) {
1343 if (!((chip->config[i] & PM8058_IRQF_MASK_ALL)
1344 == PM8058_IRQF_MASK_ALL)) {
1345 data = irq_get_irq_data(i +
1346 chip->pdata.irq_base);
1347 pm8058_irq_bus_lock(data);
1348 pm8058_irq_unmask(data);
1349 pm8058_irq_bus_sync_unlock(data);
1350 }
1351 }
1352 }
1353
1354 if (!chip->count_wakeable)
1355 enable_irq(chip->dev->irq);
1356}
1357
1358static struct syscore_ops pm8058_pm = {
1359 .suspend = pm8058_suspend,
1360 .resume = pm8058_resume,
1361};
1362
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001363static int pm8058_probe(struct i2c_client *client,
1364 const struct i2c_device_id *id)
1365{
1366 int i, rc;
1367 struct pm8058_platform_data *pdata = client->dev.platform_data;
1368 struct pm8058_chip *chip;
1369
1370 if (pdata == NULL || !client->irq) {
1371 pr_err("%s: No platform_data or IRQ.\n", __func__);
1372 return -ENODEV;
1373 }
1374
1375 if (pdata->num_subdevs == 0) {
1376 pr_err("%s: No sub devices to support.\n", __func__);
1377 return -ENODEV;
1378 }
1379
1380 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C) == 0) {
1381 pr_err("%s: i2c_check_functionality failed.\n", __func__);
1382 return -ENODEV;
1383 }
1384
1385 chip = kzalloc(sizeof *chip, GFP_KERNEL);
1386 if (chip == NULL) {
1387 pr_err("%s: kzalloc() failed.\n", __func__);
1388 return -ENOMEM;
1389 }
1390
1391 chip->dev = client;
1392
1393 /* Read PMIC chip revision */
1394 rc = ssbi_read(chip->dev, SSBI_REG_REV, &chip->revision, 1);
1395 if (rc)
1396 pr_err("%s: Failed on ssbi_read for revision: rc=%d.\n",
1397 __func__, rc);
1398 pr_info("%s: PMIC revision: %X\n", __func__, chip->revision);
1399
1400 (void) memcpy((void *)&chip->pdata, (const void *)pdata,
1401 sizeof(chip->pdata));
1402
1403 mutex_init(&chip->pm_lock);
1404 irq_set_handler_data(chip->dev->irq, (void *)chip);
1405 irq_set_irq_wake(chip->dev->irq, 1);
1406
1407 chip->pm_max_irq = 0;
1408 chip->pm_max_blocks = 0;
1409 chip->pm_max_masters = 0;
1410
1411 i2c_set_clientdata(client, chip);
1412
1413 pmic_chip = chip;
1414
1415 /* Register for all reserved IRQs */
1416 for (i = pdata->irq_base; i < (pdata->irq_base + MAX_PM_IRQ); i++) {
1417 irq_set_chip(i, &pm8058_irq_chip);
1418 irq_set_chip_data(i, (void *)chip);
1419 irq_set_handler(i, handle_edge_irq);
1420 set_irq_flags(i, IRQF_VALID);
1421 irq_set_nested_thread(i, 1);
1422 }
1423
1424 rc = mfd_add_devices(&chip->dev->dev, 0, pdata->sub_devices,
1425 pdata->num_subdevs, NULL, 0);
1426
1427 /* Add charger sub device with the chip parameter as driver data */
1428 if (pdata->charger_sub_device) {
1429 rc = mfd_add_devices(&chip->dev->dev, 0,
1430 pdata->charger_sub_device,
1431 1, NULL, 0);
1432 }
1433
1434 if (pdata->init) {
1435 rc = pdata->init(chip);
1436 if (rc != 0) {
1437 pr_err("%s: board init failed\n", __func__);
1438 chip->dev = NULL;
1439 kfree(chip);
1440 return -ENODEV;
1441 }
1442 }
1443
1444 rc = request_threaded_irq(chip->dev->irq, NULL, pm8058_isr_thread,
1445 IRQF_ONESHOT | IRQF_DISABLED | pdata->irq_trigger_flags,
1446 "pm8058-irq", chip);
1447 if (rc < 0)
1448 pr_err("%s: could not request irq %d: %d\n", __func__,
1449 chip->dev->irq, rc);
1450
1451 rc = pmic8058_dbg_probe(chip);
1452 if (rc < 0)
1453 pr_err("%s: could not set up debugfs: %d\n", __func__, rc);
1454
Abhijeet Dharmapurikara3c0d942011-07-25 17:53:45 -07001455 register_syscore_ops(&pm8058_pm);
1456
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001457 return 0;
1458}
1459
1460static int __devexit pm8058_remove(struct i2c_client *client)
1461{
1462 struct pm8058_chip *chip;
1463
1464 chip = i2c_get_clientdata(client);
1465 if (chip) {
1466 if (chip->pm_max_irq) {
1467 irq_set_irq_wake(chip->dev->irq, 0);
1468 free_irq(chip->dev->irq, chip);
1469 }
1470 mutex_destroy(&chip->pm_lock);
1471 chip->dev = NULL;
1472
1473 kfree(chip);
1474 }
1475
1476 pmic8058_dbg_remove();
1477
1478 return 0;
1479}
1480
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001481static const struct i2c_device_id pm8058_ids[] = {
1482 { "pm8058-core", 0 },
1483 { },
1484};
1485MODULE_DEVICE_TABLE(i2c, pm8058_ids);
1486
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001487static struct i2c_driver pm8058_driver = {
1488 .driver.name = "pm8058-core",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001489 .id_table = pm8058_ids,
1490 .probe = pm8058_probe,
1491 .remove = __devexit_p(pm8058_remove),
1492};
1493
1494static int __init pm8058_init(void)
1495{
1496 int rc = i2c_add_driver(&pm8058_driver);
1497 pr_notice("%s: i2c_add_driver: rc = %d\n", __func__, rc);
1498
1499 return rc;
1500}
1501
1502static void __exit pm8058_exit(void)
1503{
1504 i2c_del_driver(&pm8058_driver);
1505}
1506
1507arch_initcall(pm8058_init);
1508module_exit(pm8058_exit);
1509
1510MODULE_LICENSE("GPL v2");
1511MODULE_DESCRIPTION("PMIC8058 core driver");
1512MODULE_VERSION("1.0");
1513MODULE_ALIAS("platform:pmic8058-core");