1 /* Philips/NXP PCF50606 Power Management Unit (PMU) driver
3 * (C) 2006-2007 by Openmoko, Inc.
4 * Authors: Harald Welte <laforge@openmoko.org>,
5 * Matt Hsu <matt@openmoko.org>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23 * This driver is a monster ;) It provides the following features
24 * - voltage control for a dozen different voltage domains
25 * - charging control for main and backup battery
28 * - adc driver (hw_sensors like)
34 #include <linux/module.h>
35 #include <linux/init.h>
36 #include <linux/i2c.h>
37 #include <linux/types.h>
38 #include <linux/interrupt.h>
39 #include <linux/irq.h>
40 #include <linux/workqueue.h>
41 #include <linux/delay.h>
42 #include <linux/rtc.h>
43 #include <linux/bcd.h>
44 #include <linux/watchdog.h>
45 #include <linux/miscdevice.h>
46 #include <linux/input.h>
48 #include <linux/backlight.h>
49 #include <linux/sched.h>
50 #include <linux/platform_device.h>
51 #include <linux/pcf50606.h>
52 #include <linux/apm-emulation.h>
53 #include <linux/power_supply.h>
55 #include <asm/mach-types.h>
56 #include <asm/arch/gta01.h>
60 /* we use dev_dbg() throughout the code, but sometimes don't want to
61 * write an entire line of debug related information. This DEBUGPC
62 * macro is a continuation for dev_dbg() */
64 #define DEBUGPC(x, args ...) printk(x, ## args)
66 #define DEBUGPC(x, args ...)
69 /***********************************************************************
70 * Static data / structures
71 ***********************************************************************/
73 static unsigned short normal_i2c
[] = { 0x08, I2C_CLIENT_END
};
75 I2C_CLIENT_INSMOD_1(pcf50606
);
77 #define PCF50606_B_CHG_FAST 0 /* Charger Fast allowed */
78 #define PCF50606_B_CHG_PRESENT 1 /* Charger present */
79 #define PCF50606_B_CHG_FOK 2 /* Fast OK for battery */
80 #define PCF50606_B_CHG_ERR 3 /* Charger Error */
81 #define PCF50606_B_CHG_PROT 4 /* Charger Protection */
82 #define PCF50606_B_CHG_READY 5 /* Charging completed */
84 #define PCF50606_F_CHG_FAST (1<<PCF50606_B_CHG_FAST)
85 #define PCF50606_F_CHG_PRESENT (1<<PCF50606_B_CHG_PRESENT)
86 #define PCF50606_F_CHG_FOK (1<<PCF50606_B_CHG_FOK)
87 #define PCF50606_F_CHG_ERR (1<<PCF50606_B_CHG_ERR)
88 #define PCF50606_F_CHG_PROT (1<<PCF50606_B_CHG_PROT)
89 #define PCF50606_F_CHG_READY (1<<PCF50606_B_CHG_READY)
90 #define PCF50606_F_CHG_MASK 0x000000fc
92 #define PCF50606_F_PWR_PRESSED 0x00000100
93 #define PCF50606_F_RTC_SECOND 0x00000200
97 CLOSE_STATE_ALLOW
= 0x2342,
100 enum pcf50606_suspend_states
{
102 PCF50606_SS_STARTING_SUSPEND
,
103 PCF50606_SS_COMPLETED_SUSPEND
,
104 PCF50606_SS_RESUMING_BUT_NOT_US_YET
,
105 PCF50606_SS_STARTING_RESUME
,
106 PCF50606_SS_COMPLETED_RESUME
,
109 struct pcf50606_data
{
110 struct i2c_client client
;
111 struct pcf50606_platform_data
*pdata
;
112 struct backlight_device
*backlight
;
115 unsigned int working
;
116 struct mutex working_lock
;
117 struct work_struct work
;
118 struct rtc_device
*rtc
;
119 struct input_dev
*input_dev
;
124 int suppress_onkey_events
;
125 enum pcf50606_suspend_states suspend_state
;
128 u_int8_t dcdc1
, dcdc2
;
136 u_int8_t adcc1
, adcc2
;
138 u_int8_t int1m
, int2m
, int3m
;
143 static struct i2c_driver pcf50606_driver
;
145 /* This global is set by the pcf50606 driver to the correct callback
146 * for the gta01 battery driver. */
147 int (*pmu_bat_get_property
)(struct power_supply
*, enum power_supply_property
,
148 union power_supply_propval
*);
149 EXPORT_SYMBOL(pmu_bat_get_property
);
151 /* This is an ugly construct on how to access the (currently single/global)
152 * pcf50606 handle from other code in the kernel. I didn't really come up with
153 * a more decent method of dynamically resolving this */
154 struct pcf50606_data
*pcf50606_global
;
155 EXPORT_SYMBOL_GPL(pcf50606_global
);
157 static struct platform_device
*pcf50606_pdev
;
159 /* This is a 10k, B=3370 NTC Thermistor -10..79 centigrade */
160 /* Table entries are offset by +0.5C so a properly rounded value is generated */
161 static const u_int16_t ntc_table_10k_3370B
[] = {
163 43888, 41819, 39862, 38010, 36257, 34596, 33024, 31534, 30121, 28781,
164 27510, 26304, 25159, 24071, 23038, 22056, 21122, 20234, 19390, 18586,
165 17821, 17093, 16399, 15738, 15107, 14506, 13933, 13387, 12865, 12367,
166 11891, 11437, 11003, 10588, 10192, 9813, 9450, 9103, 8771, 8453,
167 8149, 7857, 7578, 7310, 7054, 6808, 6572, 6346, 6129, 5920,
168 5720, 5528, 5344, 5167, 4996, 4833, 4675, 4524, 4379, 4239,
169 4104, 3975, 3850, 3730, 3614, 3503, 3396, 3292, 3193, 3097,
170 3004, 2915, 2829, 2745, 2665, 2588, 2513, 2441, 2371, 2304,
171 2239, 2176, 2116, 2057, 2000, 1945, 1892, 1841, 1791, 1743,
175 /***********************************************************************
177 ***********************************************************************/
179 static inline int __reg_write(struct pcf50606_data
*pcf
, u_int8_t reg
,
182 if (pcf
->suspend_state
== PCF50606_SS_COMPLETED_SUSPEND
) {
183 dev_err(&pcf
->client
.dev
, "__reg_write while suspended.\n");
186 return i2c_smbus_write_byte_data(&pcf
->client
, reg
, val
);
189 static int reg_write(struct pcf50606_data
*pcf
, u_int8_t reg
, u_int8_t val
)
193 mutex_lock(&pcf
->lock
);
194 ret
= __reg_write(pcf
, reg
, val
);
195 mutex_unlock(&pcf
->lock
);
200 static inline int32_t __reg_read(struct pcf50606_data
*pcf
, u_int8_t reg
)
204 if (pcf
->suspend_state
== PCF50606_SS_COMPLETED_SUSPEND
) {
205 dev_err(&pcf
->client
.dev
, "__reg_read while suspended.\n");
208 ret
= i2c_smbus_read_byte_data(&pcf
->client
, reg
);
213 static u_int8_t
reg_read(struct pcf50606_data
*pcf
, u_int8_t reg
)
217 mutex_lock(&pcf
->lock
);
218 ret
= __reg_read(pcf
, reg
);
219 mutex_unlock(&pcf
->lock
);
224 static int reg_set_bit_mask(struct pcf50606_data
*pcf
,
225 u_int8_t reg
, u_int8_t mask
, u_int8_t val
)
232 mutex_lock(&pcf
->lock
);
234 tmp
= __reg_read(pcf
, reg
);
237 ret
= __reg_write(pcf
, reg
, tmp
);
239 mutex_unlock(&pcf
->lock
);
244 static int reg_clear_bits(struct pcf50606_data
*pcf
, u_int8_t reg
, u_int8_t val
)
249 mutex_lock(&pcf
->lock
);
251 tmp
= __reg_read(pcf
, reg
);
253 ret
= __reg_write(pcf
, reg
, tmp
);
255 mutex_unlock(&pcf
->lock
);
260 /* synchronously read one ADC channel (busy-wait for result to be complete) */
261 static u_int16_t
adc_read(struct pcf50606_data
*pcf
, int channel
,
264 u_int8_t adcs2
, adcs1
;
267 dev_dbg(&pcf
->client
.dev
, "entering (pcf=%p, channel=%u, data2=%p)\n",
268 pcf
, channel
, data2
);
270 channel
&= PCF50606_ADCC2_ADCMUX_MASK
;
272 mutex_lock(&pcf
->lock
);
274 /* start ADC conversion of selected channel */
275 __reg_write(pcf
, PCF50606_REG_ADCC2
, channel
|
276 PCF50606_ADCC2_ADCSTART
| PCF50606_ADCC2_RES_10BIT
);
279 adcs2
= __reg_read(pcf
, PCF50606_REG_ADCS2
);
280 } while (!(adcs2
& PCF50606_ADCS2_ADCRDY
));
282 adcs1
= __reg_read(pcf
, PCF50606_REG_ADCS1
);
283 ret
= (adcs1
<< 2) | (adcs2
& 0x03);
286 adcs1
= __reg_read(pcf
, PCF50606_REG_ADCS3
);
287 *data2
= (adcs1
<< 2) | ((adcs2
& 0x0c) >> 2);
290 mutex_unlock(&pcf
->lock
);
292 dev_dbg(&pcf
->client
.dev
, "returning %u %u\n", ret
,
298 /***********************************************************************
300 ***********************************************************************/
302 static u_int8_t
dcudc_voltage(unsigned int millivolts
)
304 if (millivolts
< 900)
306 if (millivolts
> 5500)
308 if (millivolts
<= 3300) {
310 return millivolts
/300;
312 if (millivolts
< 4000)
316 return millivolts
/100;
320 static unsigned int dcudc_2voltage(u_int8_t bits
)
324 return 900 + bits
* 300;
325 else if (bits
< 0x10)
328 return 4000 + bits
* 100;
331 static u_int8_t
dcdec_voltage(unsigned int millivolts
)
333 if (millivolts
< 900)
335 else if (millivolts
> 3300)
339 return millivolts
/300;
342 static unsigned int dcdec_2voltage(u_int8_t bits
)
345 return 900 + bits
*300;
348 static u_int8_t
dcdc_voltage(unsigned int millivolts
)
350 if (millivolts
< 900)
352 else if (millivolts
> 3600)
355 if (millivolts
< 1500) {
357 return millivolts
/25;
360 return 0x18 + millivolts
/300;
364 static unsigned int dcdc_2voltage(u_int8_t bits
)
367 if ((bits
& 0x18) == 0x18)
368 return 1500 + ((bits
& 0x7) * 300);
370 return 900 + (bits
* 25);
373 static u_int8_t
dx_voltage(unsigned int millivolts
)
375 if (millivolts
< 900)
377 else if (millivolts
> 3300)
381 return millivolts
/100;
384 static unsigned int dx_2voltage(u_int8_t bits
)
387 return 900 + (bits
* 100);
390 static const u_int8_t regulator_registers
[__NUM_PCF50606_REGULATORS
] = {
391 [PCF50606_REGULATOR_DCD
] = PCF50606_REG_DCDC1
,
392 [PCF50606_REGULATOR_DCDE
] = PCF50606_REG_DCDEC1
,
393 [PCF50606_REGULATOR_DCUD
] = PCF50606_REG_DCUDC1
,
394 [PCF50606_REGULATOR_D1REG
] = PCF50606_REG_D1REGC1
,
395 [PCF50606_REGULATOR_D2REG
] = PCF50606_REG_D2REGC1
,
396 [PCF50606_REGULATOR_D3REG
] = PCF50606_REG_D3REGC1
,
397 [PCF50606_REGULATOR_LPREG
] = PCF50606_REG_LPREGC1
,
398 [PCF50606_REGULATOR_IOREG
] = PCF50606_REG_IOREGC
,
401 int pcf50606_onoff_set(struct pcf50606_data
*pcf
,
402 enum pcf50606_regulator_id reg
, int on
)
406 if (reg
>= __NUM_PCF50606_REGULATORS
)
409 /* IOREG cannot be powered off since it powers the PMU I2C */
410 if (reg
== PCF50606_REGULATOR_IOREG
)
413 addr
= regulator_registers
[reg
];
416 reg_set_bit_mask(pcf
, addr
, 0xe0, 0x00);
418 reg_set_bit_mask(pcf
, addr
, 0xe0, 0xe0);
422 EXPORT_SYMBOL_GPL(pcf50606_onoff_set
);
424 int pcf50606_onoff_get(struct pcf50606_data
*pcf
,
425 enum pcf50606_regulator_id reg
)
429 if (reg
>= __NUM_PCF50606_REGULATORS
)
432 addr
= regulator_registers
[reg
];
433 val
= (reg_read(pcf
, addr
) & 0xe0) >> 5;
435 /* PWREN1 = 1, PWREN2 = 1, see table 16 of datasheet */
444 EXPORT_SYMBOL_GPL(pcf50606_onoff_get
);
446 int pcf50606_voltage_set(struct pcf50606_data
*pcf
,
447 enum pcf50606_regulator_id reg
,
448 unsigned int millivolts
)
454 dev_dbg(&pcf
->client
.dev
, "pcf=%p, reg=%d, mvolts=%d\n", pcf
, reg
,
457 if (reg
>= __NUM_PCF50606_REGULATORS
)
460 if (millivolts
> pcf
->pdata
->rails
[reg
].voltage
.max
)
464 case PCF50606_REGULATOR_DCD
:
465 volt_bits
= dcdc_voltage(millivolts
);
466 rc
= reg_set_bit_mask(pcf
, PCF50606_REG_DCDC1
, 0x1f,
469 case PCF50606_REGULATOR_DCDE
:
470 volt_bits
= dcdec_voltage(millivolts
);
471 rc
= reg_set_bit_mask(pcf
, PCF50606_REG_DCDEC1
, 0x0f,
474 case PCF50606_REGULATOR_DCUD
:
475 volt_bits
= dcudc_voltage(millivolts
);
476 rc
= reg_set_bit_mask(pcf
, PCF50606_REG_DCUDC1
, 0x1f,
479 case PCF50606_REGULATOR_D1REG
:
480 case PCF50606_REGULATOR_D2REG
:
481 case PCF50606_REGULATOR_D3REG
:
482 regnr
= PCF50606_REG_D1REGC1
+ (reg
- PCF50606_REGULATOR_D1REG
);
483 volt_bits
= dx_voltage(millivolts
);
484 rc
= reg_set_bit_mask(pcf
, regnr
, 0x1f, volt_bits
);
486 case PCF50606_REGULATOR_LPREG
:
487 volt_bits
= dx_voltage(millivolts
);
488 rc
= reg_set_bit_mask(pcf
, PCF50606_REG_LPREGC1
, 0x1f,
491 case PCF50606_REGULATOR_IOREG
:
492 if (millivolts
< 1800)
494 volt_bits
= dx_voltage(millivolts
);
495 rc
= reg_set_bit_mask(pcf
, PCF50606_REG_IOREGC
, 0x1f,
504 EXPORT_SYMBOL_GPL(pcf50606_voltage_set
);
506 unsigned int pcf50606_voltage_get(struct pcf50606_data
*pcf
,
507 enum pcf50606_regulator_id reg
)
513 if (reg
>= __NUM_PCF50606_REGULATORS
)
517 case PCF50606_REGULATOR_DCD
:
518 volt_bits
= reg_read(pcf
, PCF50606_REG_DCDC1
) & 0x1f;
519 rc
= dcdc_2voltage(volt_bits
);
521 case PCF50606_REGULATOR_DCDE
:
522 volt_bits
= reg_read(pcf
, PCF50606_REG_DCDEC1
) & 0x0f;
523 rc
= dcdec_2voltage(volt_bits
);
525 case PCF50606_REGULATOR_DCUD
:
526 volt_bits
= reg_read(pcf
, PCF50606_REG_DCUDC1
) & 0x1f;
527 rc
= dcudc_2voltage(volt_bits
);
529 case PCF50606_REGULATOR_D1REG
:
530 case PCF50606_REGULATOR_D2REG
:
531 case PCF50606_REGULATOR_D3REG
:
532 regnr
= PCF50606_REG_D1REGC1
+ (reg
- PCF50606_REGULATOR_D1REG
);
533 volt_bits
= reg_read(pcf
, regnr
) & 0x1f;
534 if (volt_bits
> 0x18)
536 rc
= dx_2voltage(volt_bits
);
538 case PCF50606_REGULATOR_LPREG
:
539 volt_bits
= reg_read(pcf
, PCF50606_REG_LPREGC1
) & 0x1f;
540 if (volt_bits
> 0x18)
542 rc
= dx_2voltage(volt_bits
);
544 case PCF50606_REGULATOR_IOREG
:
545 volt_bits
= reg_read(pcf
, PCF50606_REG_IOREGC
) & 0x1f;
546 if (volt_bits
> 0x18)
548 rc
= dx_2voltage(volt_bits
);
556 EXPORT_SYMBOL_GPL(pcf50606_voltage_get
);
558 /* go into 'STANDBY' mode, i.e. power off the main CPU and peripherals */
559 void pcf50606_go_standby(void)
561 reg_write(pcf50606_global
, PCF50606_REG_OOCC1
,
562 PCF50606_OOCC1_GOSTDBY
);
564 EXPORT_SYMBOL_GPL(pcf50606_go_standby
);
566 void pcf50606_gpo0_set(struct pcf50606_data
*pcf
, int on
)
575 reg_set_bit_mask(pcf
, PCF50606_REG_GPOC1
, 0x0f, val
);
577 EXPORT_SYMBOL_GPL(pcf50606_gpo0_set
);
579 int pcf50606_gpo0_get(struct pcf50606_data
*pcf
)
581 u_int8_t reg
= reg_read(pcf
, PCF50606_REG_GPOC1
) & 0x0f;
583 if (reg
== 0x07 || reg
== 0x08)
588 EXPORT_SYMBOL_GPL(pcf50606_gpo0_get
);
590 static void pcf50606_work(struct work_struct
*work
)
592 struct pcf50606_data
*pcf
=
593 container_of(work
, struct pcf50606_data
, work
);
597 mutex_lock(&pcf
->working_lock
);
601 if (!&pcf
->client
.dev
)
605 * if we are presently suspending, we are not in a position to deal
606 * with pcf50606 interrupts at all.
608 * Because we didn't clear the int pending registers, there will be
609 * no edge / interrupt waiting for us when we wake. But it is OK
610 * because at the end of our resume, we call this workqueue function
611 * gratuitously, clearing the pending register and re-enabling
612 * servicing this interrupt.
615 if ((pcf
->suspend_state
== PCF50606_SS_STARTING_SUSPEND
) ||
616 (pcf
->suspend_state
== PCF50606_SS_COMPLETED_SUSPEND
))
620 * If we are inside suspend -> resume completion time we don't attempt
621 * service until we have fully resumed. Although we could talk to the
622 * device as soon as I2C is up, the regs in the device which we might
623 * choose to modify as part of the service action have not been
624 * reloaded with their pre-suspend states yet. Therefore we will
625 * defer our service if we are called like that until our resume has
628 * This shouldn't happen any more because we disable servicing this
629 * interrupt in suspend and don't re-enable it until resume is
633 if (pcf
->suspend_state
&&
634 (pcf
->suspend_state
!= PCF50606_SS_COMPLETED_RESUME
))
637 /* this is the case early in resume! Sanity check! */
638 if (i2c_get_clientdata(&pcf
->client
) == NULL
)
642 * p35 pcf50606 datasheet rev 2.2:
643 * ''The system controller shall read all interrupt registers in
644 * one I2C read action''
645 * because if you don't INT# gets stuck asserted forever after a
648 ret
= i2c_smbus_read_i2c_block_data(&pcf
->client
, PCF50606_REG_INT1
,
649 sizeof(pcfirq
), pcfirq
);
650 if (ret
!= sizeof(pcfirq
)) {
651 DEBUGPC("Oh crap PMU IRQ register read failed %d\n", ret
);
653 * it shouldn't fail, we no longer attempt to use
654 * I2C while it can be suspended. But we don't have
655 * much option but to retry if if it ever did fail,
656 * because if we don't service the interrupt to clear
657 * it, we will never see another PMU interrupt edge.
662 /* hey did we just resume? (because we don't get here unless we are
663 * running normally or the first call after resumption)
665 * pcf50606 resume is really really over now then.
667 if (pcf
->suspend_state
!= PCF50606_SS_RUNNING
) {
668 pcf
->suspend_state
= PCF50606_SS_RUNNING
;
670 /* peek at the IRQ reason, if power button then set a flag
671 * so that we do not signal the event to userspace
673 if (pcfirq
[0] & (PCF50606_INT1_ONKEYF
| PCF50606_INT1_ONKEYR
)) {
674 pcf
->suppress_onkey_events
= 1;
675 dev_dbg(&pcf
->client
.dev
,
676 "Wake by ONKEY, suppressing ONKEY events");
678 pcf
->suppress_onkey_events
= 0;
682 if (!pcf
->coldplug_done
) {
683 DEBUGPC("PMU Coldplug init\n");
685 /* we used SECOND to kick ourselves started -- turn it off */
686 pcfirq
[0] &= ~PCF50606_INT1_SECOND
;
687 reg_set_bit_mask(pcf
, PCF50606_REG_INT1M
, PCF50606_INT1_SECOND
,
688 PCF50606_INT1_SECOND
);
690 /* coldplug the USB if present */
691 if (__reg_read(pcf
, PCF50606_REG_OOCS
) & PCF50606_OOCS_EXTON
) {
692 /* Charger inserted */
693 DEBUGPC("COLD CHGINS ");
694 input_report_key(pcf
->input_dev
, KEY_BATTERY
, 1);
695 apm_queue_event(APM_POWER_STATUS_CHANGE
);
696 pcf
->flags
|= PCF50606_F_CHG_PRESENT
;
698 pcf
->pdata
->cb(&pcf
->client
.dev
,
703 pcf
->coldplug_done
= 1;
707 dev_dbg(&pcf
->client
.dev
, "INT1=0x%02x INT2=0x%02x INT3=0x%02x:",
708 pcfirq
[0], pcfirq
[1], pcfirq
[2]);
710 if (pcfirq
[0] & PCF50606_INT1_ONKEYF
) {
711 /* ONKEY falling edge (start of button press) */
712 pcf
->flags
|= PCF50606_F_PWR_PRESSED
;
713 if (!pcf
->suppress_onkey_events
) {
715 input_report_key(pcf
->input_dev
, KEY_POWER
, 1);
717 DEBUGPC("ONKEYF(unreported) ");
720 if (pcfirq
[0] & PCF50606_INT1_ONKEY1S
) {
721 /* ONKEY pressed for more than 1 second */
722 pcf
->onkey_seconds
= 0;
724 /* Tell PMU we are taking care of this */
725 reg_set_bit_mask(pcf
, PCF50606_REG_OOCC1
,
726 PCF50606_OOCC1_TOTRST
,
727 PCF50606_OOCC1_TOTRST
);
728 /* enable SECOND interrupt (hz tick) */
729 reg_clear_bits(pcf
, PCF50606_REG_INT1M
, PCF50606_INT1_SECOND
);
731 if (pcfirq
[0] & PCF50606_INT1_ONKEYR
) {
732 /* ONKEY rising edge (end of button press) */
733 pcf
->flags
&= ~PCF50606_F_PWR_PRESSED
;
734 pcf
->onkey_seconds
= -1;
735 if (!pcf
->suppress_onkey_events
) {
737 input_report_key(pcf
->input_dev
, KEY_POWER
, 0);
739 DEBUGPC("ONKEYR(suppressed) ");
740 /* don't suppress any more power button events */
741 pcf
->suppress_onkey_events
= 0;
743 /* disable SECOND interrupt in case RTC didn't
745 if (!(pcf
->flags
& PCF50606_F_RTC_SECOND
))
746 reg_set_bit_mask(pcf
, PCF50606_REG_INT1M
,
747 PCF50606_INT1_SECOND
,
748 PCF50606_INT1_SECOND
);
750 if (pcfirq
[0] & PCF50606_INT1_EXTONR
) {
752 input_report_key(pcf
->input_dev
, KEY_POWER2
, 1);
754 if (pcfirq
[0] & PCF50606_INT1_EXTONF
) {
756 input_report_key(pcf
->input_dev
, KEY_POWER2
, 0);
758 if (pcfirq
[0] & PCF50606_INT1_SECOND
) {
760 if (pcf
->flags
& PCF50606_F_RTC_SECOND
)
761 rtc_update_irq(pcf
->rtc
, 1,
764 if (pcf
->onkey_seconds
>= 0 &&
765 pcf
->flags
& PCF50606_F_PWR_PRESSED
) {
766 DEBUGPC("ONKEY_SECONDS(%u, OOCC1=0x%02x) ",
768 reg_read(pcf
, PCF50606_REG_OOCC1
));
769 pcf
->onkey_seconds
++;
770 if (pcf
->onkey_seconds
>=
771 pcf
->pdata
->onkey_seconds_required
) {
772 /* Ask init to do 'ctrlaltdel' */
774 * currently Linux reacts badly to issuing a
775 * signal to PID #1 before init is started.
776 * What happens is that the next kernel thread
777 * to start, which is the JFFS2 Garbage
778 * collector in our case, gets the signal
779 * instead and proceeds to fail to fork --
780 * which is very bad. Therefore we confirm
781 * PID #1 exists before issuing the signal
783 if (find_task_by_pid(1)) {
784 kill_proc(1, SIGINT
, 1);
785 DEBUGPC("SIGINT(init) ");
787 /* FIXME: what to do if userspace doesn't
788 * shut down? Do we want to force it? */
792 if (pcfirq
[0] & PCF50606_INT1_ALARM
) {
794 if (pcf
->pdata
->used_features
& PCF50606_FEAT_RTC
)
795 rtc_update_irq(pcf
->rtc
, 1,
799 if (pcfirq
[1] & PCF50606_INT2_CHGINS
) {
800 /* Charger inserted */
802 input_report_key(pcf
->input_dev
, KEY_BATTERY
, 1);
803 apm_queue_event(APM_POWER_STATUS_CHANGE
);
804 pcf
->flags
|= PCF50606_F_CHG_PRESENT
;
806 pcf
->pdata
->cb(&pcf
->client
.dev
,
807 PCF50606_FEAT_MBC
, PMU_EVT_INSERT
);
808 /* FIXME: how to signal this to userspace */
810 if (pcfirq
[1] & PCF50606_INT2_CHGRM
) {
811 /* Charger removed */
813 input_report_key(pcf
->input_dev
, KEY_BATTERY
, 0);
814 apm_queue_event(APM_POWER_STATUS_CHANGE
);
815 pcf
->flags
&= ~(PCF50606_F_CHG_MASK
|PCF50606_F_CHG_PRESENT
);
817 pcf
->pdata
->cb(&pcf
->client
.dev
,
818 PCF50606_FEAT_MBC
, PMU_EVT_INSERT
);
819 /* FIXME: how signal this to userspace */
821 if (pcfirq
[1] & PCF50606_INT2_CHGFOK
) {
822 /* Battery ready for fast charging */
824 pcf
->flags
|= PCF50606_F_CHG_FOK
;
825 /* FIXME: how to signal this to userspace */
827 if (pcfirq
[1] & PCF50606_INT2_CHGERR
) {
828 /* Error in charge mode */
830 pcf
->flags
|= PCF50606_F_CHG_ERR
;
831 pcf
->flags
&= ~(PCF50606_F_CHG_FOK
|PCF50606_F_CHG_READY
);
832 /* FIXME: how to signal this to userspace */
834 if (pcfirq
[1] & PCF50606_INT2_CHGFRDY
) {
835 /* Fast charge completed */
837 pcf
->flags
|= PCF50606_F_CHG_READY
;
838 pcf
->flags
&= ~PCF50606_F_CHG_FOK
;
839 /* FIXME: how to signal this to userspace */
841 if (pcfirq
[1] & PCF50606_INT2_CHGPROT
) {
842 /* Charging protection interrupt */
844 pcf
->flags
&= ~(PCF50606_F_CHG_FOK
|PCF50606_F_CHG_READY
);
845 /* FIXME: signal this to userspace */
847 if (pcfirq
[1] & PCF50606_INT2_CHGWD10S
) {
848 /* Charger watchdog will expire in 10 seconds */
849 DEBUGPC("CHGWD10S ");
850 reg_set_bit_mask(pcf
, PCF50606_REG_OOCC1
,
851 PCF50606_OOCC1_WDTRST
,
852 PCF50606_OOCC1_WDTRST
);
854 if (pcfirq
[1] & PCF50606_INT2_CHGWDEXP
) {
855 /* Charger watchdog expires */
856 DEBUGPC("CHGWDEXP ");
857 /* FIXME: how to signal this to userspace */
860 if (pcfirq
[2] & PCF50606_INT3_ADCRDY
) {
861 /* ADC result ready */
864 if (pcfirq
[2] & PCF50606_INT3_ACDINS
) {
865 /* Accessory insertion detected */
868 pcf
->pdata
->cb(&pcf
->client
.dev
,
869 PCF50606_FEAT_ACD
, PMU_EVT_INSERT
);
871 if (pcfirq
[2] & PCF50606_INT3_ACDREM
) {
872 /* Accessory removal detected */
875 pcf
->pdata
->cb(&pcf
->client
.dev
,
876 PCF50606_FEAT_ACD
, PMU_EVT_REMOVE
);
879 if (pcfirq
[2] & PCF50606_INT3_LOWBAT
) {
880 if (__reg_read(pcf
, PCF50606_REG_OOCS
) & PCF50606_OOCS_EXTON
) {
882 * hey no need to freak out, we have some kind of
883 * valid charger power
887 /* Really low battery voltage, we have 8 seconds left */
890 * currently Linux reacts badly to issuing a signal to
891 * PID #1 before init is started. What happens is that
892 * the next kernel thread to start, which is the JFFS2
893 * Garbage collector in our case, gets the signal
894 * instead and proceeds to fail to fork -- which is
895 * very bad. Therefore we confirm PID #1 exists
896 * before issuing SPIGPWR
898 if (find_task_by_pid(1)) {
899 apm_queue_event(APM_LOW_BATTERY
);
900 DEBUGPC("SIGPWR(init) ");
901 kill_proc(1, SIGPWR
, 1);
904 * well, our situation is like this: we do not
905 * have any external power, we have a low
906 * battery and since PID #1 doesn't exist yet,
907 * we are early in the boot, likely before
908 * rootfs mount. We should just call it a day
910 apm_queue_event(APM_CRITICAL_SUSPEND
);
912 /* Tell PMU we are taking care of this */
913 reg_set_bit_mask(pcf
, PCF50606_REG_OOCC1
,
914 PCF50606_OOCC1_TOTRST
,
915 PCF50606_OOCC1_TOTRST
);
917 if (pcfirq
[2] & PCF50606_INT3_HIGHTMP
) {
918 /* High temperature */
920 apm_queue_event(APM_CRITICAL_SUSPEND
);
927 input_sync(pcf
->input_dev
);
928 put_device(&pcf
->client
.dev
);
929 mutex_unlock(&pcf
->working_lock
);
935 if ((pcf
->suspend_state
!= PCF50606_SS_STARTING_SUSPEND
) &&
936 (pcf
->suspend_state
!= PCF50606_SS_COMPLETED_SUSPEND
)) {
938 dev_info(&pcf
->client
.dev
, "rescheduling interrupt service\n");
940 if (!schedule_work(&pcf
->work
))
941 dev_err(&pcf
->client
.dev
, "int service reschedule failed\n");
943 /* we don't put the device here, hold it for next time */
944 mutex_unlock(&pcf
->working_lock
);
947 static irqreturn_t
pcf50606_irq(int irq
, void *_pcf
)
949 struct pcf50606_data
*pcf
= _pcf
;
951 dev_dbg(&pcf
->client
.dev
, "entering(irq=%u, pcf=%p): scheduling work\n",
953 get_device(&pcf
->client
.dev
);
954 if (!schedule_work(&pcf
->work
) && !pcf
->working
)
955 dev_err(&pcf
->client
.dev
, "pcf irq work already queued.\n");
960 static u_int16_t
adc_to_batt_millivolts(u_int16_t adc
)
964 mvolts
= (adc
* 6000) / 1024;
969 #define BATTVOLT_SCALE_START 2800
970 #define BATTVOLT_SCALE_END 4200
971 #define BATTVOLT_SCALE_DIVIDER ((BATTVOLT_SCALE_END - BATTVOLT_SCALE_START)/100)
973 static u_int8_t
battvolt_scale(u_int16_t battvolt
)
975 /* FIXME: this linear scale is completely bogus */
976 u_int16_t battvolt_relative
= battvolt
- BATTVOLT_SCALE_START
;
977 unsigned int percent
= battvolt_relative
/ BATTVOLT_SCALE_DIVIDER
;
982 u_int16_t
pcf50606_battvolt(struct pcf50606_data
*pcf
)
985 adc
= adc_read(pcf
, PCF50606_ADCMUX_BATVOLT_RES
, NULL
);
987 return adc_to_batt_millivolts(adc
);
989 EXPORT_SYMBOL_GPL(pcf50606_battvolt
);
991 static ssize_t
show_battvolt(struct device
*dev
, struct device_attribute
*attr
,
994 struct i2c_client
*client
= to_i2c_client(dev
);
995 struct pcf50606_data
*pcf
= i2c_get_clientdata(client
);
997 return sprintf(buf
, "%u\n", pcf50606_battvolt(pcf
));
999 static DEVICE_ATTR(battvolt
, S_IRUGO
| S_IWUSR
, show_battvolt
, NULL
);
1001 static int reg_id_by_name(const char *name
)
1005 if (!strcmp(name
, "voltage_dcd"))
1006 reg_id
= PCF50606_REGULATOR_DCD
;
1007 else if (!strcmp(name
, "voltage_dcde"))
1008 reg_id
= PCF50606_REGULATOR_DCDE
;
1009 else if (!strcmp(name
, "voltage_dcud"))
1010 reg_id
= PCF50606_REGULATOR_DCUD
;
1011 else if (!strcmp(name
, "voltage_d1reg"))
1012 reg_id
= PCF50606_REGULATOR_D1REG
;
1013 else if (!strcmp(name
, "voltage_d2reg"))
1014 reg_id
= PCF50606_REGULATOR_D2REG
;
1015 else if (!strcmp(name
, "voltage_d3reg"))
1016 reg_id
= PCF50606_REGULATOR_D3REG
;
1017 else if (!strcmp(name
, "voltage_lpreg"))
1018 reg_id
= PCF50606_REGULATOR_LPREG
;
1019 else if (!strcmp(name
, "voltage_ioreg"))
1020 reg_id
= PCF50606_REGULATOR_IOREG
;
1027 static ssize_t
show_vreg(struct device
*dev
, struct device_attribute
*attr
,
1030 struct i2c_client
*client
= to_i2c_client(dev
);
1031 struct pcf50606_data
*pcf
= i2c_get_clientdata(client
);
1032 unsigned int reg_id
;
1034 reg_id
= reg_id_by_name(attr
->attr
.name
);
1038 if (pcf50606_onoff_get(pcf
, reg_id
) > 0)
1039 return sprintf(buf
, "%u\n", pcf50606_voltage_get(pcf
, reg_id
));
1041 return strlcpy(buf
, "0\n", PAGE_SIZE
);
1044 static ssize_t
set_vreg(struct device
*dev
, struct device_attribute
*attr
,
1045 const char *buf
, size_t count
)
1047 struct i2c_client
*client
= to_i2c_client(dev
);
1048 struct pcf50606_data
*pcf
= i2c_get_clientdata(client
);
1049 unsigned long mvolts
= simple_strtoul(buf
, NULL
, 10);
1050 unsigned int reg_id
;
1052 reg_id
= reg_id_by_name(attr
->attr
.name
);
1056 dev_dbg(dev
, "attempting to set %s(%d) to %lu mvolts\n",
1057 attr
->attr
.name
, reg_id
, mvolts
);
1060 pcf50606_onoff_set(pcf
, reg_id
, 0);
1062 if (pcf50606_voltage_set(pcf
, reg_id
, mvolts
) < 0) {
1063 dev_warn(dev
, "refusing to set %s(%d) to %lu mvolts "
1064 "(max=%u)\n", attr
->attr
.name
, reg_id
, mvolts
,
1065 pcf
->pdata
->rails
[reg_id
].voltage
.max
);
1068 pcf50606_onoff_set(pcf
, reg_id
, 1);
1074 static DEVICE_ATTR(voltage_dcd
, S_IRUGO
| S_IWUSR
, show_vreg
, set_vreg
);
1075 static DEVICE_ATTR(voltage_dcde
, S_IRUGO
| S_IWUSR
, show_vreg
, set_vreg
);
1076 static DEVICE_ATTR(voltage_dcud
, S_IRUGO
| S_IWUSR
, show_vreg
, set_vreg
);
1077 static DEVICE_ATTR(voltage_d1reg
, S_IRUGO
| S_IWUSR
, show_vreg
, set_vreg
);
1078 static DEVICE_ATTR(voltage_d2reg
, S_IRUGO
| S_IWUSR
, show_vreg
, set_vreg
);
1079 static DEVICE_ATTR(voltage_d3reg
, S_IRUGO
| S_IWUSR
, show_vreg
, set_vreg
);
1080 static DEVICE_ATTR(voltage_lpreg
, S_IRUGO
| S_IWUSR
, show_vreg
, set_vreg
);
1081 static DEVICE_ATTR(voltage_ioreg
, S_IRUGO
| S_IWUSR
, show_vreg
, set_vreg
);
1083 /***********************************************************************
1085 ***********************************************************************/
1087 /* Enable/disable fast charging (500mA in the GTA01) */
1088 void pcf50606_charge_fast(struct pcf50606_data
*pcf
, int on
)
1090 if (!(pcf
->pdata
->used_features
& PCF50606_FEAT_MBC
))
1094 /* We can allow PCF to automatically charge
1096 pcf
->flags
|= PCF50606_F_CHG_FAST
;
1097 reg_set_bit_mask(pcf
, PCF50606_REG_MBCC1
,
1098 PCF50606_MBCC1_AUTOFST
,
1099 PCF50606_MBCC1_AUTOFST
);
1101 pcf
->flags
&= ~PCF50606_F_CHG_FAST
;
1102 /* disable automatic fast-charge */
1103 reg_clear_bits(pcf
, PCF50606_REG_MBCC1
,
1104 PCF50606_MBCC1_AUTOFST
);
1105 /* switch to idle mode to abort existing charge
1107 reg_set_bit_mask(pcf
, PCF50606_REG_MBCC1
,
1108 PCF50606_MBCC1_CHGMOD_MASK
,
1109 PCF50606_MBCC1_CHGMOD_IDLE
);
1112 EXPORT_SYMBOL_GPL(pcf50606_charge_fast
);
1114 static inline u_int16_t
adc_to_rntc(struct pcf50606_data
*pcf
, u_int16_t adc
)
1116 u_int32_t r_ntc
= (adc
* (u_int32_t
)pcf
->pdata
->r_fix_batt
)
1122 static inline int16_t rntc_to_temp(u_int16_t rntc
)
1126 for (i
= 0; i
< ARRAY_SIZE(ntc_table_10k_3370B
); i
++) {
1127 if (rntc
> ntc_table_10k_3370B
[i
])
1128 return i
- 10; /* First element is -10 */
1130 return -99; /* Below our range */
1133 static ssize_t
show_battemp(struct device
*dev
, struct device_attribute
*attr
,
1136 struct i2c_client
*client
= to_i2c_client(dev
);
1137 struct pcf50606_data
*pcf
= i2c_get_clientdata(client
);
1140 adc
= adc_read(pcf
, PCF50606_ADCMUX_BATTEMP
, NULL
);
1142 return sprintf(buf
, "%d\n", rntc_to_temp(adc_to_rntc(pcf
, adc
)));
1144 static DEVICE_ATTR(battemp
, S_IRUGO
| S_IWUSR
, show_battemp
, NULL
);
1146 static inline int16_t adc_to_chg_milliamps(struct pcf50606_data
*pcf
,
1147 u_int16_t adc_adcin1
,
1148 u_int16_t adc_batvolt
)
1150 int32_t res
= (adc_adcin1
- adc_batvolt
) * 2400;
1151 return (res
* 1000) / (pcf
->pdata
->r_sense_milli
* 1024);
1154 static ssize_t
show_chgcur(struct device
*dev
, struct device_attribute
*attr
,
1157 struct i2c_client
*client
= to_i2c_client(dev
);
1158 struct pcf50606_data
*pcf
= i2c_get_clientdata(client
);
1159 u_int16_t adc_batvolt
, adc_adcin1
;
1162 adc_batvolt
= adc_read(pcf
, PCF50606_ADCMUX_BATVOLT_ADCIN1
,
1164 ma
= adc_to_chg_milliamps(pcf
, adc_adcin1
, adc_batvolt
);
1166 return sprintf(buf
, "%d\n", ma
);
1168 static DEVICE_ATTR(chgcur
, S_IRUGO
| S_IWUSR
, show_chgcur
, NULL
);
1170 static const char *chgmode_names
[] = {
1171 [PCF50606_MBCC1_CHGMOD_QUAL
] = "qualification",
1172 [PCF50606_MBCC1_CHGMOD_PRE
] = "pre",
1173 [PCF50606_MBCC1_CHGMOD_TRICKLE
] = "trickle",
1174 [PCF50606_MBCC1_CHGMOD_FAST_CCCV
] = "fast_cccv",
1175 [PCF50606_MBCC1_CHGMOD_FAST_NOCC
] = "fast_nocc",
1176 [PCF50606_MBCC1_CHGMOD_FAST_NOCV
] = "fast_nocv",
1177 [PCF50606_MBCC1_CHGMOD_FAST_SW
] = "fast_switch",
1178 [PCF50606_MBCC1_CHGMOD_IDLE
] = "idle",
1181 static ssize_t
show_chgmode(struct device
*dev
, struct device_attribute
*attr
,
1184 struct i2c_client
*client
= to_i2c_client(dev
);
1185 struct pcf50606_data
*pcf
= i2c_get_clientdata(client
);
1186 u_int8_t mbcc1
= reg_read(pcf
, PCF50606_REG_MBCC1
);
1187 u_int8_t chgmod
= (mbcc1
& PCF50606_MBCC1_CHGMOD_MASK
);
1189 return sprintf(buf
, "%s\n", chgmode_names
[chgmod
]);
1192 static ssize_t
set_chgmode(struct device
*dev
, struct device_attribute
*attr
,
1193 const char *buf
, size_t count
)
1195 struct i2c_client
*client
= to_i2c_client(dev
);
1196 struct pcf50606_data
*pcf
= i2c_get_clientdata(client
);
1197 u_int8_t mbcc1
= reg_read(pcf
, PCF50606_REG_MBCC1
);
1199 mbcc1
&= ~PCF50606_MBCC1_CHGMOD_MASK
;
1201 if (!strcmp(buf
, "qualification"))
1202 mbcc1
|= PCF50606_MBCC1_CHGMOD_QUAL
;
1203 else if (!strcmp(buf
, "pre"))
1204 mbcc1
|= PCF50606_MBCC1_CHGMOD_PRE
;
1205 else if (!strcmp(buf
, "trickle"))
1206 mbcc1
|= PCF50606_MBCC1_CHGMOD_TRICKLE
;
1207 else if (!strcmp(buf
, "fast_cccv"))
1208 mbcc1
|= PCF50606_MBCC1_CHGMOD_FAST_CCCV
;
1209 /* We don't allow the other fast modes for security reasons */
1210 else if (!strcmp(buf
, "idle"))
1211 mbcc1
|= PCF50606_MBCC1_CHGMOD_IDLE
;
1215 reg_write(pcf
, PCF50606_REG_MBCC1
, mbcc1
);
1220 static DEVICE_ATTR(chgmode
, S_IRUGO
| S_IWUSR
, show_chgmode
, set_chgmode
);
1222 static const char *chgstate_names
[] = {
1223 [PCF50606_B_CHG_FAST
] = "fast_enabled",
1224 [PCF50606_B_CHG_PRESENT
] = "present",
1225 [PCF50606_B_CHG_FOK
] = "fast_ok",
1226 [PCF50606_B_CHG_ERR
] = "error",
1227 [PCF50606_B_CHG_PROT
] = "protection",
1228 [PCF50606_B_CHG_READY
] = "ready",
1231 static ssize_t
show_chgstate(struct device
*dev
, struct device_attribute
*attr
,
1234 struct i2c_client
*client
= to_i2c_client(dev
);
1235 struct pcf50606_data
*pcf
= i2c_get_clientdata(client
);
1239 for (i
= 0; i
< 32; i
++)
1240 if (pcf
->flags
& (1 << i
) && i
< ARRAY_SIZE(chgstate_names
))
1241 b
+= sprintf(b
, "%s ", chgstate_names
[i
]);
1244 b
+= sprintf(b
, "\n");
1248 static DEVICE_ATTR(chgstate
, S_IRUGO
| S_IWUSR
, show_chgstate
, NULL
);
1250 /***********************************************************************
1252 ***********************************************************************/
1254 static void pcf50606_get_power_status(struct apm_power_info
*info
)
1256 struct pcf50606_data
*pcf
= pcf50606_global
;
1257 u_int8_t mbcc1
= reg_read(pcf
, PCF50606_REG_MBCC1
);
1258 u_int8_t chgmod
= mbcc1
& PCF50606_MBCC1_CHGMOD_MASK
;
1259 u_int16_t battvolt
= pcf50606_battvolt(pcf
);
1261 if (reg_read(pcf
, PCF50606_REG_OOCS
) & PCF50606_OOCS_EXTON
)
1262 info
->ac_line_status
= APM_AC_ONLINE
;
1264 info
->ac_line_status
= APM_AC_OFFLINE
;
1267 case PCF50606_MBCC1_CHGMOD_QUAL
:
1268 case PCF50606_MBCC1_CHGMOD_PRE
:
1269 case PCF50606_MBCC1_CHGMOD_IDLE
:
1270 info
->battery_life
= battvolt_scale(battvolt
);
1273 info
->battery_status
= APM_BATTERY_STATUS_CHARGING
;
1274 info
->battery_flag
= APM_BATTERY_FLAG_CHARGING
;
1279 /***********************************************************************
1280 * Battery driver interface
1281 ***********************************************************************/
1282 static int pcf50606_bat_get_property(struct power_supply
*psy
,
1283 enum power_supply_property psp
,
1284 union power_supply_propval
*val
)
1286 u_int16_t adc
, adc_adcin1
;
1287 u_int8_t mbcc1
, chgmod
;
1288 struct pcf50606_data
*pcf
= pcf50606_global
;
1293 case POWER_SUPPLY_PROP_STATUS
:
1294 if (!(reg_read(pcf
, PCF50606_REG_OOCS
) & PCF50606_OOCS_EXTON
)) {
1295 /* No charger, clearly we're discharging then */
1296 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
1299 /* We have a charger present, get charge mode */
1300 mbcc1
= reg_read(pcf
, PCF50606_REG_MBCC1
);
1301 chgmod
= (mbcc1
& PCF50606_MBCC1_CHGMOD_MASK
);
1304 /* TODO: How to determine POWER_SUPPLY_STATUS_FULL? */
1306 case PCF50606_MBCC1_CHGMOD_QUAL
:
1307 case PCF50606_MBCC1_CHGMOD_PRE
:
1308 case PCF50606_MBCC1_CHGMOD_IDLE
:
1309 val
->intval
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
1312 case PCF50606_MBCC1_CHGMOD_TRICKLE
:
1313 case PCF50606_MBCC1_CHGMOD_FAST_CCCV
:
1314 case PCF50606_MBCC1_CHGMOD_FAST_NOCC
:
1315 case PCF50606_MBCC1_CHGMOD_FAST_NOCV
:
1316 case PCF50606_MBCC1_CHGMOD_FAST_SW
:
1317 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
1321 val
->intval
= POWER_SUPPLY_STATUS_UNKNOWN
;
1327 case POWER_SUPPLY_PROP_PRESENT
:
1328 val
->intval
= 1; /* Must be, or the magic smoke comes out */
1331 case POWER_SUPPLY_PROP_ONLINE
:
1332 val
->intval
= !!(reg_read(pcf
, PCF50606_REG_OOCS
) &
1333 PCF50606_OOCS_EXTON
);
1336 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
1337 adc
= adc_read(pcf
, PCF50606_ADCMUX_BATVOLT_RES
, NULL
);
1338 /* (adc * 6000000) / 1024 == (adc * 46875) / 8 */
1339 val
->intval
= (adc
* 46875) / 8;
1342 case POWER_SUPPLY_PROP_CURRENT_NOW
:
1343 adc
= adc_read(pcf
, PCF50606_ADCMUX_BATVOLT_ADCIN1
,
1345 val
->intval
= adc_to_chg_milliamps(pcf
, adc_adcin1
, adc
) * 1000;
1348 case POWER_SUPPLY_PROP_TEMP
:
1349 adc
= adc_read(pcf
, PCF50606_ADCMUX_BATTEMP
, NULL
);
1350 val
->intval
= rntc_to_temp(adc_to_rntc(pcf
, adc
)) * 10;
1353 case POWER_SUPPLY_PROP_CAPACITY
:
1354 val
->intval
= battvolt_scale(pcf50606_battvolt(pcf
));
1365 /***********************************************************************
1367 ***********************************************************************/
1369 struct pcf50606_time
{
1379 static void pcf2rtc_time(struct rtc_time
*rtc
, struct pcf50606_time
*pcf
)
1381 rtc
->tm_sec
= BCD2BIN(pcf
->sec
);
1382 rtc
->tm_min
= BCD2BIN(pcf
->min
);
1383 rtc
->tm_hour
= BCD2BIN(pcf
->hour
);
1384 rtc
->tm_wday
= BCD2BIN(pcf
->wkday
);
1385 rtc
->tm_mday
= BCD2BIN(pcf
->day
);
1386 rtc
->tm_mon
= BCD2BIN(pcf
->month
);
1387 rtc
->tm_year
= BCD2BIN(pcf
->year
) + 100;
1390 static void rtc2pcf_time(struct pcf50606_time
*pcf
, struct rtc_time
*rtc
)
1392 pcf
->sec
= BIN2BCD(rtc
->tm_sec
);
1393 pcf
->min
= BIN2BCD(rtc
->tm_min
);
1394 pcf
->hour
= BIN2BCD(rtc
->tm_hour
);
1395 pcf
->wkday
= BIN2BCD(rtc
->tm_wday
);
1396 pcf
->day
= BIN2BCD(rtc
->tm_mday
);
1397 pcf
->month
= BIN2BCD(rtc
->tm_mon
);
1398 pcf
->year
= BIN2BCD(rtc
->tm_year
- 100);
1401 static int pcf50606_rtc_ioctl(struct device
*dev
, unsigned int cmd
,
1404 struct i2c_client
*client
= to_i2c_client(dev
);
1405 struct pcf50606_data
*pcf
= i2c_get_clientdata(client
);
1409 /* disable the alarm interrupt */
1410 reg_set_bit_mask(pcf
, PCF50606_REG_INT1M
,
1411 PCF50606_INT1_ALARM
, PCF50606_INT1_ALARM
);
1414 /* enable the alarm interrupt */
1415 reg_clear_bits(pcf
, PCF50606_REG_INT1M
, PCF50606_INT1_ALARM
);
1418 /* disable periodic interrupt (hz tick) */
1419 pcf
->flags
&= ~PCF50606_F_RTC_SECOND
;
1420 reg_set_bit_mask(pcf
, PCF50606_REG_INT1M
,
1421 PCF50606_INT1_SECOND
, PCF50606_INT1_SECOND
);
1424 /* ensable periodic interrupt (hz tick) */
1425 pcf
->flags
|= PCF50606_F_RTC_SECOND
;
1426 reg_clear_bits(pcf
, PCF50606_REG_INT1M
, PCF50606_INT1_SECOND
);
1429 return -ENOIOCTLCMD
;
1432 static int pcf50606_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
1434 struct i2c_client
*client
= to_i2c_client(dev
);
1435 struct pcf50606_data
*pcf
= i2c_get_clientdata(client
);
1436 struct pcf50606_time pcf_tm
;
1438 mutex_lock(&pcf
->lock
);
1439 pcf_tm
.sec
= __reg_read(pcf
, PCF50606_REG_RTCSC
);
1440 pcf_tm
.min
= __reg_read(pcf
, PCF50606_REG_RTCMN
);
1441 pcf_tm
.hour
= __reg_read(pcf
, PCF50606_REG_RTCHR
);
1442 pcf_tm
.wkday
= __reg_read(pcf
, PCF50606_REG_RTCWD
);
1443 pcf_tm
.day
= __reg_read(pcf
, PCF50606_REG_RTCDT
);
1444 pcf_tm
.month
= __reg_read(pcf
, PCF50606_REG_RTCMT
);
1445 pcf_tm
.year
= __reg_read(pcf
, PCF50606_REG_RTCYR
);
1446 mutex_unlock(&pcf
->lock
);
1448 dev_dbg(dev
, "PCF_TIME: %02x.%02x.%02x %02x:%02x:%02x\n",
1449 pcf_tm
.day
, pcf_tm
.month
, pcf_tm
.year
,
1450 pcf_tm
.hour
, pcf_tm
.min
, pcf_tm
.sec
);
1452 pcf2rtc_time(tm
, &pcf_tm
);
1454 dev_dbg(dev
, "RTC_TIME: %u.%u.%u %u:%u:%u\n",
1455 tm
->tm_mday
, tm
->tm_mon
, tm
->tm_year
,
1456 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
1461 static int pcf50606_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
1463 struct i2c_client
*client
= to_i2c_client(dev
);
1464 struct pcf50606_data
*pcf
= i2c_get_clientdata(client
);
1465 struct pcf50606_time pcf_tm
;
1468 dev_dbg(dev
, "RTC_TIME: %u.%u.%u %u:%u:%u\n",
1469 tm
->tm_mday
, tm
->tm_mon
, tm
->tm_year
,
1470 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
1471 rtc2pcf_time(&pcf_tm
, tm
);
1472 dev_dbg(dev
, "PCF_TIME: %02x.%02x.%02x %02x:%02x:%02x\n",
1473 pcf_tm
.day
, pcf_tm
.month
, pcf_tm
.year
,
1474 pcf_tm
.hour
, pcf_tm
.min
, pcf_tm
.sec
);
1476 mutex_lock(&pcf
->lock
);
1478 /* disable SECOND interrupt */
1479 int1m
= __reg_read(pcf
, PCF50606_REG_INT1M
);
1480 __reg_write(pcf
, PCF50606_REG_INT1M
, int1m
| PCF50606_INT1_SECOND
);
1482 __reg_write(pcf
, PCF50606_REG_RTCSC
, pcf_tm
.sec
);
1483 __reg_write(pcf
, PCF50606_REG_RTCMN
, pcf_tm
.min
);
1484 __reg_write(pcf
, PCF50606_REG_RTCHR
, pcf_tm
.hour
);
1485 __reg_write(pcf
, PCF50606_REG_RTCWD
, pcf_tm
.wkday
);
1486 __reg_write(pcf
, PCF50606_REG_RTCDT
, pcf_tm
.day
);
1487 __reg_write(pcf
, PCF50606_REG_RTCMT
, pcf_tm
.month
);
1488 __reg_write(pcf
, PCF50606_REG_RTCYR
, pcf_tm
.year
);
1490 /* restore INT1M, potentially re-enable SECOND interrupt */
1491 __reg_write(pcf
, PCF50606_REG_INT1M
, int1m
);
1493 mutex_unlock(&pcf
->lock
);
1498 static int pcf50606_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
1500 struct i2c_client
*client
= to_i2c_client(dev
);
1501 struct pcf50606_data
*pcf
= i2c_get_clientdata(client
);
1502 struct pcf50606_time pcf_tm
;
1504 mutex_lock(&pcf
->lock
);
1506 __reg_read(pcf
, PCF50606_REG_INT1M
) & PCF50606_INT1_ALARM
1508 pcf_tm
.sec
= __reg_read(pcf
, PCF50606_REG_RTCSCA
);
1509 pcf_tm
.min
= __reg_read(pcf
, PCF50606_REG_RTCMNA
);
1510 pcf_tm
.hour
= __reg_read(pcf
, PCF50606_REG_RTCHRA
);
1511 pcf_tm
.wkday
= __reg_read(pcf
, PCF50606_REG_RTCWDA
);
1512 pcf_tm
.day
= __reg_read(pcf
, PCF50606_REG_RTCDTA
);
1513 pcf_tm
.month
= __reg_read(pcf
, PCF50606_REG_RTCMTA
);
1514 pcf_tm
.year
= __reg_read(pcf
, PCF50606_REG_RTCYRA
);
1515 mutex_unlock(&pcf
->lock
);
1517 pcf2rtc_time(&alrm
->time
, &pcf_tm
);
1522 static int pcf50606_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
1524 struct i2c_client
*client
= to_i2c_client(dev
);
1525 struct pcf50606_data
*pcf
= i2c_get_clientdata(client
);
1526 struct pcf50606_time pcf_tm
;
1529 rtc2pcf_time(&pcf_tm
, &alrm
->time
);
1531 mutex_lock(&pcf
->lock
);
1533 /* disable alarm interrupt */
1534 irqmask
= __reg_read(pcf
, PCF50606_REG_INT1M
);
1535 irqmask
|= PCF50606_INT1_ALARM
;
1536 __reg_write(pcf
, PCF50606_REG_INT1M
, irqmask
);
1538 __reg_write(pcf
, PCF50606_REG_RTCSCA
, pcf_tm
.sec
);
1539 __reg_write(pcf
, PCF50606_REG_RTCMNA
, pcf_tm
.min
);
1540 __reg_write(pcf
, PCF50606_REG_RTCHRA
, pcf_tm
.hour
);
1541 __reg_write(pcf
, PCF50606_REG_RTCWDA
, pcf_tm
.wkday
);
1542 __reg_write(pcf
, PCF50606_REG_RTCDTA
, pcf_tm
.day
);
1543 __reg_write(pcf
, PCF50606_REG_RTCMTA
, pcf_tm
.month
);
1544 __reg_write(pcf
, PCF50606_REG_RTCYRA
, pcf_tm
.year
);
1546 if (alrm
->enabled
) {
1547 /* (re-)enaable alarm interrupt */
1548 irqmask
= __reg_read(pcf
, PCF50606_REG_INT1M
);
1549 irqmask
&= ~PCF50606_INT1_ALARM
;
1550 __reg_write(pcf
, PCF50606_REG_INT1M
, irqmask
);
1553 mutex_unlock(&pcf
->lock
);
1559 static struct rtc_class_ops pcf50606_rtc_ops
= {
1560 .ioctl
= pcf50606_rtc_ioctl
,
1561 .read_time
= pcf50606_rtc_read_time
,
1562 .set_time
= pcf50606_rtc_set_time
,
1563 .read_alarm
= pcf50606_rtc_read_alarm
,
1564 .set_alarm
= pcf50606_rtc_set_alarm
,
1567 /***********************************************************************
1569 ***********************************************************************/
1571 static void pcf50606_wdt_start(struct pcf50606_data
*pcf
)
1573 reg_set_bit_mask(pcf
, PCF50606_REG_OOCC1
, PCF50606_OOCC1_WDTRST
,
1574 PCF50606_OOCC1_WDTRST
);
1577 static void pcf50606_wdt_stop(struct pcf50606_data
*pcf
)
1579 reg_clear_bits(pcf
, PCF50606_REG_OOCS
, PCF50606_OOCS_WDTEXP
);
1582 static void pcf50606_wdt_keepalive(struct pcf50606_data
*pcf
)
1584 pcf50606_wdt_start(pcf
);
1587 static int pcf50606_wdt_open(struct inode
*inode
, struct file
*file
)
1589 struct pcf50606_data
*pcf
= pcf50606_global
;
1591 file
->private_data
= pcf
;
1593 /* start the timer */
1594 pcf50606_wdt_start(pcf
);
1596 return nonseekable_open(inode
, file
);
1599 static int pcf50606_wdt_release(struct inode
*inode
, struct file
*file
)
1601 struct pcf50606_data
*pcf
= file
->private_data
;
1603 if (pcf
->allow_close
== CLOSE_STATE_ALLOW
)
1604 pcf50606_wdt_stop(pcf
);
1606 printk(KERN_CRIT
"Unexpected close, not stopping watchdog!\n");
1607 pcf50606_wdt_keepalive(pcf
);
1610 pcf
->allow_close
= CLOSE_STATE_NOT
;
1615 static ssize_t
pcf50606_wdt_write(struct file
*file
, const char __user
*data
,
1616 size_t len
, loff_t
*ppos
)
1618 struct pcf50606_data
*pcf
= file
->private_data
;
1622 for (i
= 0; i
!= len
; i
++) {
1624 if (get_user(c
, data
+ i
))
1627 pcf
->allow_close
= CLOSE_STATE_ALLOW
;
1629 pcf50606_wdt_keepalive(pcf
);
1635 static struct watchdog_info pcf50606_wdt_ident
= {
1636 .options
= WDIOF_MAGICCLOSE
,
1637 .firmware_version
= 0,
1638 .identity
= "PCF50606 Watchdog",
1641 static int pcf50606_wdt_ioctl(struct inode
*inode
, struct file
*file
,
1642 unsigned int cmd
, unsigned long arg
)
1644 struct pcf50606_data
*pcf
= file
->private_data
;
1645 void __user
*argp
= (void __user
*)arg
;
1646 int __user
*p
= argp
;
1649 case WDIOC_GETSUPPORT
:
1650 return copy_to_user(argp
, &pcf50606_wdt_ident
,
1651 sizeof(pcf50606_wdt_ident
)) ? -EFAULT
: 0;
1653 case WDIOC_GETSTATUS
:
1654 case WDIOC_GETBOOTSTATUS
:
1655 return put_user(0, p
);
1656 case WDIOC_KEEPALIVE
:
1657 pcf50606_wdt_keepalive(pcf
);
1659 case WDIOC_GETTIMEOUT
:
1660 return put_user(8, p
);
1662 return -ENOIOCTLCMD
;
1666 static struct file_operations pcf50606_wdt_fops
= {
1667 .owner
= THIS_MODULE
,
1668 .llseek
= no_llseek
,
1669 .write
= &pcf50606_wdt_write
,
1670 .ioctl
= &pcf50606_wdt_ioctl
,
1671 .open
= &pcf50606_wdt_open
,
1672 .release
= &pcf50606_wdt_release
,
1675 static struct miscdevice pcf50606_wdt_miscdev
= {
1676 .minor
= WATCHDOG_MINOR
,
1678 .fops
= &pcf50606_wdt_fops
,
1681 /***********************************************************************
1683 ***********************************************************************/
1685 static const char *pwm_dc_table
[] = {
1686 "0/16", "1/16", "2/16", "3/16",
1687 "4/16", "5/16", "6/16", "7/16",
1688 "8/16", "9/16", "10/16", "11/16",
1689 "12/16", "13/16", "14/16", "15/16",
1692 static ssize_t
show_pwm_dc(struct device
*dev
, struct device_attribute
*attr
,
1695 struct i2c_client
*client
= to_i2c_client(dev
);
1696 struct pcf50606_data
*pcf
= i2c_get_clientdata(client
);
1699 val
= reg_read(pcf
, PCF50606_REG_PWMC1
) >> PCF50606_PWMC1_DC_SHIFT
;
1702 return sprintf(buf
, "%s\n", pwm_dc_table
[val
]);
1705 static ssize_t
set_pwm_dc(struct device
*dev
, struct device_attribute
*attr
,
1706 const char *buf
, size_t count
)
1708 struct i2c_client
*client
= to_i2c_client(dev
);
1709 struct pcf50606_data
*pcf
= i2c_get_clientdata(client
);
1712 for (i
= 0; i
< ARRAY_SIZE(pwm_dc_table
); i
++) {
1713 if (!strncmp(buf
, pwm_dc_table
[i
], strlen(pwm_dc_table
[i
]))) {
1714 dev_dbg(dev
, "setting pwm dc %s\n\r", pwm_dc_table
[i
]);
1715 reg_set_bit_mask(pcf
, PCF50606_REG_PWMC1
, 0x1e,
1716 (i
<< PCF50606_PWMC1_DC_SHIFT
));
1722 static DEVICE_ATTR(pwm_dc
, S_IRUGO
| S_IWUSR
, show_pwm_dc
, set_pwm_dc
);
1724 static const char *pwm_clk_table
[] = {
1725 "512", "256", "128", "64",
1726 "56300", "28100", "14100", "7000",
1729 static ssize_t
show_pwm_clk(struct device
*dev
, struct device_attribute
*attr
,
1732 struct i2c_client
*client
= to_i2c_client(dev
);
1733 struct pcf50606_data
*pcf
= i2c_get_clientdata(client
);
1736 val
= reg_read(pcf
, PCF50606_REG_PWMC1
) >> PCF50606_PWMC1_CLK_SHIFT
;
1739 return sprintf(buf
, "%s\n", pwm_clk_table
[val
]);
1742 static ssize_t
set_pwm_clk(struct device
*dev
, struct device_attribute
*attr
,
1743 const char *buf
, size_t count
)
1745 struct i2c_client
*client
= to_i2c_client(dev
);
1746 struct pcf50606_data
*pcf
= i2c_get_clientdata(client
);
1749 for (i
= 0; i
< ARRAY_SIZE(pwm_clk_table
); i
++) {
1750 if (!strncmp(buf
, pwm_clk_table
[i
], strlen(pwm_clk_table
[i
]))) {
1751 dev_dbg(dev
, "setting pwm clk %s\n\r",
1753 reg_set_bit_mask(pcf
, PCF50606_REG_PWMC1
, 0xe0,
1754 (i
<< PCF50606_PWMC1_CLK_SHIFT
));
1760 static DEVICE_ATTR(pwm_clk
, S_IRUGO
| S_IWUSR
, show_pwm_clk
, set_pwm_clk
);
1762 static int pcf50606bl_get_intensity(struct backlight_device
*bd
)
1764 struct pcf50606_data
*pcf
= bl_get_data(bd
);
1765 int intensity
= reg_read(pcf
, PCF50606_REG_PWMC1
);
1766 intensity
= (intensity
>> PCF50606_PWMC1_DC_SHIFT
);
1768 return intensity
& 0xf;
1771 static int pcf50606bl_set_intensity(struct backlight_device
*bd
)
1773 struct pcf50606_data
*pcf
= bl_get_data(bd
);
1774 int intensity
= bd
->props
.brightness
;
1776 if (bd
->props
.power
!= FB_BLANK_UNBLANK
)
1778 if (bd
->props
.fb_blank
!= FB_BLANK_UNBLANK
)
1781 return reg_set_bit_mask(pcf
, PCF50606_REG_PWMC1
, 0x1e,
1782 (intensity
<< PCF50606_PWMC1_DC_SHIFT
));
1785 static struct backlight_ops pcf50606bl_ops
= {
1786 .get_brightness
= pcf50606bl_get_intensity
,
1787 .update_status
= pcf50606bl_set_intensity
,
1790 /***********************************************************************
1791 * Driver initialization
1792 ***********************************************************************/
1794 #ifdef CONFIG_MACH_NEO1973_GTA01
1795 /* We currently place those platform devices here to make sure the device
1796 * suspend/resume order is correct */
1797 static struct platform_device gta01_pm_gps_dev
= {
1798 .name
= "neo1973-pm-gps",
1801 static struct platform_device gta01_pm_bt_dev
= {
1802 .name
= "neo1973-pm-bt",
1806 static struct attribute
*pcf_sysfs_entries
[16] = {
1807 &dev_attr_voltage_dcd
.attr
,
1808 &dev_attr_voltage_dcde
.attr
,
1809 &dev_attr_voltage_dcud
.attr
,
1810 &dev_attr_voltage_d1reg
.attr
,
1811 &dev_attr_voltage_d2reg
.attr
,
1812 &dev_attr_voltage_d3reg
.attr
,
1813 &dev_attr_voltage_lpreg
.attr
,
1814 &dev_attr_voltage_ioreg
.attr
,
1818 static struct attribute_group pcf_attr_group
= {
1819 .name
= NULL
, /* put in device directory */
1820 .attrs
= pcf_sysfs_entries
,
1823 static void populate_sysfs_group(struct pcf50606_data
*pcf
)
1826 struct attribute
**attr
;
1828 for (attr
= pcf_sysfs_entries
; *attr
; attr
++)
1831 if (pcf
->pdata
->used_features
& PCF50606_FEAT_MBC
) {
1832 pcf_sysfs_entries
[i
++] = &dev_attr_chgstate
.attr
;
1833 pcf_sysfs_entries
[i
++] = &dev_attr_chgmode
.attr
;
1836 if (pcf
->pdata
->used_features
& PCF50606_FEAT_CHGCUR
)
1837 pcf_sysfs_entries
[i
++] = &dev_attr_chgcur
.attr
;
1839 if (pcf
->pdata
->used_features
& PCF50606_FEAT_BATVOLT
)
1840 pcf_sysfs_entries
[i
++] = &dev_attr_battvolt
.attr
;
1842 if (pcf
->pdata
->used_features
& PCF50606_FEAT_BATTEMP
)
1843 pcf_sysfs_entries
[i
++] = &dev_attr_battemp
.attr
;
1845 if (pcf
->pdata
->used_features
& PCF50606_FEAT_PWM
) {
1846 pcf_sysfs_entries
[i
++] = &dev_attr_pwm_dc
.attr
;
1847 pcf_sysfs_entries
[i
++] = &dev_attr_pwm_clk
.attr
;
1851 static int pcf50606_detect(struct i2c_adapter
*adapter
, int address
, int kind
)
1853 struct i2c_client
*new_client
;
1854 struct pcf50606_data
*data
;
1858 if (!pcf50606_pdev
) {
1859 printk(KERN_ERR
"pcf50606: driver needs a platform_device!\n");
1863 irq
= platform_get_irq(pcf50606_pdev
, 0);
1865 dev_err(&pcf50606_pdev
->dev
, "no irq in platform resources!\n");
1869 /* At the moment, we only support one PCF50606 in a system */
1870 if (pcf50606_global
) {
1871 dev_err(&pcf50606_pdev
->dev
,
1872 "currently only one chip supported\n");
1876 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
1880 mutex_init(&data
->lock
);
1881 mutex_init(&data
->working_lock
);
1882 INIT_WORK(&data
->work
, pcf50606_work
);
1885 data
->suppress_onkey_events
= 0;
1886 data
->onkey_seconds
= -1;
1887 data
->pdata
= pcf50606_pdev
->dev
.platform_data
;
1889 new_client
= &data
->client
;
1890 i2c_set_clientdata(new_client
, data
);
1891 new_client
->addr
= address
;
1892 new_client
->adapter
= adapter
;
1893 new_client
->driver
= &pcf50606_driver
;
1894 new_client
->flags
= 0;
1895 strlcpy(new_client
->name
, "pcf50606", I2C_NAME_SIZE
);
1897 /* now we try to detect the chip */
1899 /* register with i2c core */
1900 err
= i2c_attach_client(new_client
);
1902 dev_err(&new_client
->dev
,
1903 "error during i2c_attach_client()\n");
1907 populate_sysfs_group(data
);
1909 err
= sysfs_create_group(&new_client
->dev
.kobj
, &pcf_attr_group
);
1911 dev_err(&new_client
->dev
, "error creating sysfs group\n");
1915 /* create virtual charger 'device' */
1917 /* input device registration */
1918 data
->input_dev
= input_allocate_device();
1919 if (!data
->input_dev
)
1922 data
->input_dev
->name
= "FIC Neo1973 PMU events";
1923 data
->input_dev
->phys
= "I2C";
1924 data
->input_dev
->id
.bustype
= BUS_I2C
;
1925 data
->input_dev
->cdev
.dev
= &new_client
->dev
;
1927 data
->input_dev
->evbit
[0] = BIT(EV_KEY
) | BIT(EV_PWR
);
1928 set_bit(KEY_POWER
, data
->input_dev
->keybit
);
1929 set_bit(KEY_POWER2
, data
->input_dev
->keybit
);
1930 set_bit(KEY_BATTERY
, data
->input_dev
->keybit
);
1932 err
= input_register_device(data
->input_dev
);
1936 /* register power off handler with core power management */
1937 pm_power_off
= &pcf50606_go_standby
;
1939 /* configure interrupt mask */
1940 /* we don't mask SECOND here, because we want one to do coldplug with */
1941 reg_write(data
, PCF50606_REG_INT1M
, 0x00);
1942 reg_write(data
, PCF50606_REG_INT2M
, 0x00);
1943 reg_write(data
, PCF50606_REG_INT3M
, PCF50606_INT3_TSCPRES
);
1945 err
= request_irq(irq
, pcf50606_irq
, IRQF_TRIGGER_FALLING
,
1950 if (enable_irq_wake(irq
) < 0)
1951 dev_err(&new_client
->dev
, "IRQ %u cannot be enabled as wake-up"
1952 "source in this hardware revision!", irq
);
1954 pcf50606_global
= data
;
1956 if (data
->pdata
->used_features
& PCF50606_FEAT_RTC
) {
1957 data
->rtc
= rtc_device_register("pcf50606", &new_client
->dev
,
1958 &pcf50606_rtc_ops
, THIS_MODULE
);
1959 if (IS_ERR(data
->rtc
)) {
1960 err
= PTR_ERR(data
->rtc
);
1965 if (data
->pdata
->used_features
& PCF50606_FEAT_WDT
) {
1966 err
= misc_register(&pcf50606_wdt_miscdev
);
1968 dev_err(&new_client
->dev
, "cannot register miscdev on "
1969 "minor=%d (%d)\n", WATCHDOG_MINOR
, err
);
1974 if (data
->pdata
->used_features
& PCF50606_FEAT_PWM
) {
1975 /* enable PWM controller */
1976 reg_set_bit_mask(data
, PCF50606_REG_PWMC1
,
1977 PCF50606_PWMC1_ACTSET
,
1978 PCF50606_PWMC1_ACTSET
);
1981 if (data
->pdata
->used_features
& PCF50606_FEAT_PWM_BL
) {
1982 data
->backlight
= backlight_device_register("pcf50606-bl",
1986 if (!data
->backlight
)
1988 data
->backlight
->props
.max_brightness
= 16;
1989 data
->backlight
->props
.power
= FB_BLANK_UNBLANK
;
1990 data
->backlight
->props
.brightness
=
1991 data
->pdata
->init_brightness
;
1992 backlight_update_status(data
->backlight
);
1995 apm_get_power_status
= pcf50606_get_power_status
;
1996 pmu_bat_get_property
= pcf50606_bat_get_property
;
1998 #ifdef CONFIG_MACH_NEO1973_GTA01
1999 if (machine_is_neo1973_gta01()) {
2000 gta01_pm_gps_dev
.dev
.parent
= &new_client
->dev
;
2001 switch (system_rev
) {
2002 case GTA01Bv2_SYSTEM_REV
:
2003 case GTA01Bv3_SYSTEM_REV
:
2004 case GTA01Bv4_SYSTEM_REV
:
2005 gta01_pm_bt_dev
.dev
.parent
= &new_client
->dev
;
2006 platform_device_register(>a01_pm_bt_dev
);
2009 platform_device_register(>a01_pm_gps_dev
);
2010 /* a link for gllin compatibility */
2011 err
= sysfs_create_link(&platform_bus_type
.devices
.kobj
,
2012 >a01_pm_gps_dev
.dev
.kobj
, "gta01-pm-gps.0");
2015 "sysfs_create_link (gta01-pm-gps.0): %d\n", err
);
2019 if (data
->pdata
->used_features
& PCF50606_FEAT_ACD
)
2020 reg_set_bit_mask(data
, PCF50606_REG_ACDC1
,
2021 PCF50606_ACDC1_ACDAPE
, PCF50606_ACDC1_ACDAPE
);
2023 reg_clear_bits(data
, PCF50606_REG_ACDC1
,
2024 PCF50606_ACDC1_ACDAPE
);
2029 if (data
->pdata
->used_features
& PCF50606_FEAT_WDT
)
2030 misc_deregister(&pcf50606_wdt_miscdev
);
2032 if (data
->pdata
->used_features
& PCF50606_FEAT_RTC
)
2033 rtc_device_unregister(pcf50606_global
->rtc
);
2035 free_irq(pcf50606_global
->irq
, pcf50606_global
);
2036 pcf50606_global
= NULL
;
2038 pm_power_off
= NULL
;
2039 input_unregister_device(data
->input_dev
);
2041 sysfs_remove_group(&new_client
->dev
.kobj
, &pcf_attr_group
);
2043 i2c_detach_client(new_client
);
2049 static int pcf50606_attach_adapter(struct i2c_adapter
*adapter
)
2051 return i2c_probe(adapter
, &addr_data
, &pcf50606_detect
);
2054 static int pcf50606_detach_client(struct i2c_client
*client
)
2056 struct pcf50606_data
*pcf
= i2c_get_clientdata(client
);
2058 apm_get_power_status
= NULL
;
2059 pmu_bat_get_property
= NULL
;
2061 input_unregister_device(pcf
->input_dev
);
2063 if (pcf
->pdata
->used_features
& PCF50606_FEAT_PWM_BL
)
2064 backlight_device_unregister(pcf
->backlight
);
2066 if (pcf
->pdata
->used_features
& PCF50606_FEAT_WDT
)
2067 misc_deregister(&pcf50606_wdt_miscdev
);
2069 if (pcf
->pdata
->used_features
& PCF50606_FEAT_RTC
)
2070 rtc_device_unregister(pcf
->rtc
);
2072 free_irq(pcf
->irq
, pcf
);
2074 sysfs_remove_group(&client
->dev
.kobj
, &pcf_attr_group
);
2076 pm_power_off
= NULL
;
2084 #define INT1M_RESUMERS (PCF50606_INT1_ALARM | \
2085 PCF50606_INT1_ONKEYF | \
2086 PCF50606_INT1_EXTONR)
2087 #define INT2M_RESUMERS (PCF50606_INT2_CHGWD10S | \
2088 PCF50606_INT2_CHGPROT | \
2089 PCF50606_INT2_CHGERR)
2090 #define INT3M_RESUMERS (PCF50606_INT3_LOWBAT | \
2091 PCF50606_INT3_HIGHTMP | \
2092 PCF50606_INT3_ACDINS)
2093 static int pcf50606_suspend(struct device
*dev
, pm_message_t state
)
2095 struct i2c_client
*client
= to_i2c_client(dev
);
2096 struct pcf50606_data
*pcf
= i2c_get_clientdata(client
);
2099 /* we suspend once (!) as late as possible in the suspend sequencing */
2101 if ((state
.event
!= PM_EVENT_SUSPEND
) ||
2102 (pcf
->suspend_state
!= PCF50606_SS_RUNNING
))
2105 /* The general idea is to power down all unused power supplies,
2106 * and then mask all PCF50606 interrup sources but EXTONR, ONKEYF
2109 mutex_lock(&pcf
->lock
);
2111 pcf
->suspend_state
= PCF50606_SS_STARTING_SUSPEND
;
2113 /* we are not going to service any further interrupts until we
2114 * resume. If the IRQ workqueue is still pending in the background,
2115 * it will bail when it sees we set suspend state above.
2118 disable_irq(pcf
->irq
);
2120 /* Save all registers that don't "survive" standby state */
2121 pcf
->standby_regs
.dcdc1
= __reg_read(pcf
, PCF50606_REG_DCDC1
);
2122 pcf
->standby_regs
.dcdc2
= __reg_read(pcf
, PCF50606_REG_DCDC2
);
2123 pcf
->standby_regs
.dcdec1
= __reg_read(pcf
, PCF50606_REG_DCDEC1
);
2124 pcf
->standby_regs
.dcudc1
= __reg_read(pcf
, PCF50606_REG_DCUDC1
);
2125 pcf
->standby_regs
.ioregc
= __reg_read(pcf
, PCF50606_REG_IOREGC
);
2126 pcf
->standby_regs
.d1regc1
= __reg_read(pcf
, PCF50606_REG_D1REGC1
);
2127 pcf
->standby_regs
.d2regc1
= __reg_read(pcf
, PCF50606_REG_D2REGC1
);
2128 pcf
->standby_regs
.d3regc1
= __reg_read(pcf
, PCF50606_REG_D3REGC1
);
2129 pcf
->standby_regs
.lpregc1
= __reg_read(pcf
, PCF50606_REG_LPREGC1
);
2130 pcf
->standby_regs
.adcc1
= __reg_read(pcf
, PCF50606_REG_ADCC1
);
2131 pcf
->standby_regs
.adcc2
= __reg_read(pcf
, PCF50606_REG_ADCC2
);
2132 pcf
->standby_regs
.pwmc1
= __reg_read(pcf
, PCF50606_REG_PWMC1
);
2134 /* switch off power supplies that are not needed during suspend */
2135 for (i
= 0; i
< __NUM_PCF50606_REGULATORS
; i
++) {
2136 if (!(pcf
->pdata
->rails
[i
].flags
& PMU_VRAIL_F_SUSPEND_ON
)) {
2139 /* IOREG powers the I@C interface so we cannot switch
2141 if (i
== PCF50606_REGULATOR_IOREG
)
2144 dev_dbg(dev
, "disabling pcf50606 regulator %u\n", i
);
2145 /* we cannot use pcf50606_onoff_set() because we're
2146 * already under the mutex */
2147 tmp
= __reg_read(pcf
, regulator_registers
[i
]);
2149 __reg_write(pcf
, regulator_registers
[i
], tmp
);
2153 pcf
->standby_regs
.int1m
= __reg_read(pcf
, PCF50606_REG_INT1M
);
2154 pcf
->standby_regs
.int2m
= __reg_read(pcf
, PCF50606_REG_INT2M
);
2155 pcf
->standby_regs
.int3m
= __reg_read(pcf
, PCF50606_REG_INT3M
);
2156 __reg_write(pcf
, PCF50606_REG_INT1M
, ~INT1M_RESUMERS
& 0xff);
2157 __reg_write(pcf
, PCF50606_REG_INT2M
, ~INT2M_RESUMERS
& 0xff);
2158 __reg_write(pcf
, PCF50606_REG_INT3M
, ~INT3M_RESUMERS
& 0xff);
2160 pcf
->suspend_state
= PCF50606_SS_COMPLETED_SUSPEND
;
2162 mutex_unlock(&pcf
->lock
);
2167 static int pcf50606_resume(struct device
*dev
)
2169 struct i2c_client
*client
= to_i2c_client(dev
);
2170 struct pcf50606_data
*pcf
= i2c_get_clientdata(client
);
2172 mutex_lock(&pcf
->lock
);
2174 pcf
->suspend_state
= PCF50606_SS_STARTING_RESUME
;
2176 /* Resume all saved registers that don't "survive" standby state */
2177 __reg_write(pcf
, PCF50606_REG_INT1M
, pcf
->standby_regs
.int1m
);
2178 __reg_write(pcf
, PCF50606_REG_INT2M
, pcf
->standby_regs
.int2m
);
2179 __reg_write(pcf
, PCF50606_REG_INT3M
, pcf
->standby_regs
.int3m
);
2181 __reg_write(pcf
, PCF50606_REG_DCDC1
, pcf
->standby_regs
.dcdc1
);
2182 __reg_write(pcf
, PCF50606_REG_DCDC2
, pcf
->standby_regs
.dcdc2
);
2183 __reg_write(pcf
, PCF50606_REG_DCDEC1
, pcf
->standby_regs
.dcdec1
);
2184 __reg_write(pcf
, PCF50606_REG_DCUDC1
, pcf
->standby_regs
.dcudc1
);
2185 __reg_write(pcf
, PCF50606_REG_IOREGC
, pcf
->standby_regs
.ioregc
);
2186 __reg_write(pcf
, PCF50606_REG_D1REGC1
, pcf
->standby_regs
.d1regc1
);
2187 __reg_write(pcf
, PCF50606_REG_D2REGC1
, pcf
->standby_regs
.d2regc1
);
2188 __reg_write(pcf
, PCF50606_REG_D3REGC1
, pcf
->standby_regs
.d3regc1
);
2189 __reg_write(pcf
, PCF50606_REG_LPREGC1
, pcf
->standby_regs
.lpregc1
);
2190 __reg_write(pcf
, PCF50606_REG_ADCC1
, pcf
->standby_regs
.adcc1
);
2191 __reg_write(pcf
, PCF50606_REG_ADCC2
, pcf
->standby_regs
.adcc2
);
2192 __reg_write(pcf
, PCF50606_REG_PWMC1
, pcf
->standby_regs
.pwmc1
);
2194 pcf
->suspend_state
= PCF50606_SS_COMPLETED_RESUME
;
2196 enable_irq(pcf
->irq
);
2198 mutex_unlock(&pcf
->lock
);
2200 /* Call PCF work function; this fixes an issue on the gta01 where
2201 * the power button "goes away" if it is used to wake the device.
2203 get_device(&pcf
->client
.dev
);
2204 pcf50606_work(&pcf
->work
);
2209 #define pcf50606_suspend NULL
2210 #define pcf50606_resume NULL
2213 static struct i2c_driver pcf50606_driver
= {
2216 .suspend
= pcf50606_suspend
,
2217 .resume
= pcf50606_resume
,
2219 .id
= I2C_DRIVERID_PCF50606
,
2220 .attach_adapter
= pcf50606_attach_adapter
,
2221 .detach_client
= pcf50606_detach_client
,
2224 /* platform driver, since i2c devices don't have platform_data */
2225 static int __init
pcf50606_plat_probe(struct platform_device
*pdev
)
2227 struct pcf50606_platform_data
*pdata
= pdev
->dev
.platform_data
;
2232 pcf50606_pdev
= pdev
;
2237 static int pcf50606_plat_remove(struct platform_device
*pdev
)
2242 /* We have this purely to capture an early indication that we are coming out
2243 * of suspend, before our device resume got called; async interrupt service is
2244 * interested in this.
2247 static int pcf50606_plat_resume(struct platform_device
*pdev
)
2249 /* i2c_get_clientdata(to_i2c_client(&pdev->dev)) returns NULL at this
2250 * early resume time so we have to use pcf50606_global
2252 pcf50606_global
->suspend_state
= PCF50606_SS_RESUMING_BUT_NOT_US_YET
;
2257 static struct platform_driver pcf50606_plat_driver
= {
2258 .probe
= pcf50606_plat_probe
,
2259 .remove
= pcf50606_plat_remove
,
2260 .resume_early
= pcf50606_plat_resume
,
2262 .owner
= THIS_MODULE
,
2267 static int __init
pcf50606_init(void)
2271 rc
= platform_driver_register(&pcf50606_plat_driver
);
2273 rc
= i2c_add_driver(&pcf50606_driver
);
2278 static void pcf50606_exit(void)
2280 i2c_del_driver(&pcf50606_driver
);
2281 platform_driver_unregister(&pcf50606_plat_driver
);
2284 MODULE_DESCRIPTION("I2C chip driver for NXP PCF50606 power management unit");
2285 MODULE_AUTHOR("Harald Welte <laforge@openmoko.org>");
2286 MODULE_LICENSE("GPL");
2288 module_init(pcf50606_init
);
2289 module_exit(pcf50606_exit
);