gta01-battery-driver.patch
[linux-2.6/openmoko-kernel/knife-kernel.git] / drivers / i2c / chips / pcf50606.c
blobf58501348147d50f535c2c1174644aacc3ed8fe3
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>
6 * All rights reserved.
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,
21 * MA 02111-1307 USA
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
26 * - rtc / alarm
27 * - watchdog
28 * - adc driver (hw_sensors like)
29 * - pwm driver
30 * - backlight
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>
47 #include <linux/fb.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>
58 #include "pcf50606.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() */
63 #ifdef DEBUG
64 #define DEBUGPC(x, args ...) printk(x, ## args)
65 #else
66 #define DEBUGPC(x, args ...)
67 #endif
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
95 enum close_state {
96 CLOSE_STATE_NOT,
97 CLOSE_STATE_ALLOW = 0x2342,
100 enum pcf50606_suspend_states {
101 PCF50606_SS_RUNNING,
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;
113 struct mutex lock;
114 unsigned int flags;
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;
120 int allow_close;
121 int onkey_seconds;
122 int irq;
123 int coldplug_done;
124 int suppress_onkey_events;
125 enum pcf50606_suspend_states suspend_state;
126 #ifdef CONFIG_PM
127 struct {
128 u_int8_t dcdc1, dcdc2;
129 u_int8_t dcdec1;
130 u_int8_t dcudc1;
131 u_int8_t ioregc;
132 u_int8_t d1regc1;
133 u_int8_t d2regc1;
134 u_int8_t d3regc1;
135 u_int8_t lpregc1;
136 u_int8_t adcc1, adcc2;
137 u_int8_t pwmc1;
138 u_int8_t int1m, int2m, int3m;
139 } standby_regs;
140 #endif
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[] = {
162 /* -10 */
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 /***********************************************************************
176 * Low-Level routines
177 ***********************************************************************/
179 static inline int __reg_write(struct pcf50606_data *pcf, u_int8_t reg,
180 u_int8_t val)
182 if (pcf->suspend_state == PCF50606_SS_COMPLETED_SUSPEND) {
183 dev_err(&pcf->client.dev, "__reg_write while suspended.\n");
184 dump_stack();
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)
191 int ret;
193 mutex_lock(&pcf->lock);
194 ret = __reg_write(pcf, reg, val);
195 mutex_unlock(&pcf->lock);
197 return ret;
200 static inline int32_t __reg_read(struct pcf50606_data *pcf, u_int8_t reg)
202 int32_t ret;
204 if (pcf->suspend_state == PCF50606_SS_COMPLETED_SUSPEND) {
205 dev_err(&pcf->client.dev, "__reg_read while suspended.\n");
206 dump_stack();
208 ret = i2c_smbus_read_byte_data(&pcf->client, reg);
210 return ret;
213 static u_int8_t reg_read(struct pcf50606_data *pcf, u_int8_t reg)
215 int32_t ret;
217 mutex_lock(&pcf->lock);
218 ret = __reg_read(pcf, reg);
219 mutex_unlock(&pcf->lock);
221 return ret & 0xff;
224 static int reg_set_bit_mask(struct pcf50606_data *pcf,
225 u_int8_t reg, u_int8_t mask, u_int8_t val)
227 int ret;
228 u_int8_t tmp;
230 val &= mask;
232 mutex_lock(&pcf->lock);
234 tmp = __reg_read(pcf, reg);
235 tmp &= ~mask;
236 tmp |= val;
237 ret = __reg_write(pcf, reg, tmp);
239 mutex_unlock(&pcf->lock);
241 return ret;
244 static int reg_clear_bits(struct pcf50606_data *pcf, u_int8_t reg, u_int8_t val)
246 int ret;
247 u_int8_t tmp;
249 mutex_lock(&pcf->lock);
251 tmp = __reg_read(pcf, reg);
252 tmp &= ~val;
253 ret = __reg_write(pcf, reg, tmp);
255 mutex_unlock(&pcf->lock);
257 return ret;
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,
262 u_int16_t *data2)
264 u_int8_t adcs2, adcs1;
265 u_int16_t ret;
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);
278 do {
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);
285 if (data2) {
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,
293 data2 ? *data2 : 0);
295 return ret;
298 /***********************************************************************
299 * Voltage / ADC
300 ***********************************************************************/
302 static u_int8_t dcudc_voltage(unsigned int millivolts)
304 if (millivolts < 900)
305 return 0;
306 if (millivolts > 5500)
307 return 0x1f;
308 if (millivolts <= 3300) {
309 millivolts -= 900;
310 return millivolts/300;
312 if (millivolts < 4000)
313 return 0x0f;
314 else {
315 millivolts -= 4000;
316 return millivolts/100;
320 static unsigned int dcudc_2voltage(u_int8_t bits)
322 bits &= 0x1f;
323 if (bits < 0x08)
324 return 900 + bits * 300;
325 else if (bits < 0x10)
326 return 3300;
327 else
328 return 4000 + bits * 100;
331 static u_int8_t dcdec_voltage(unsigned int millivolts)
333 if (millivolts < 900)
334 return 0;
335 else if (millivolts > 3300)
336 return 0x0f;
338 millivolts -= 900;
339 return millivolts/300;
342 static unsigned int dcdec_2voltage(u_int8_t bits)
344 bits &= 0x0f;
345 return 900 + bits*300;
348 static u_int8_t dcdc_voltage(unsigned int millivolts)
350 if (millivolts < 900)
351 return 0;
352 else if (millivolts > 3600)
353 return 0x1f;
355 if (millivolts < 1500) {
356 millivolts -= 900;
357 return millivolts/25;
358 } else {
359 millivolts -= 1500;
360 return 0x18 + millivolts/300;
364 static unsigned int dcdc_2voltage(u_int8_t bits)
366 bits &= 0x1f;
367 if ((bits & 0x18) == 0x18)
368 return 1500 + ((bits & 0x7) * 300);
369 else
370 return 900 + (bits * 25);
373 static u_int8_t dx_voltage(unsigned int millivolts)
375 if (millivolts < 900)
376 return 0;
377 else if (millivolts > 3300)
378 return 0x18;
380 millivolts -= 900;
381 return millivolts/100;
384 static unsigned int dx_2voltage(u_int8_t bits)
386 bits &= 0x1f;
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)
404 u_int8_t addr;
406 if (reg >= __NUM_PCF50606_REGULATORS)
407 return -EINVAL;
409 /* IOREG cannot be powered off since it powers the PMU I2C */
410 if (reg == PCF50606_REGULATOR_IOREG)
411 return -EIO;
413 addr = regulator_registers[reg];
415 if (on == 0)
416 reg_set_bit_mask(pcf, addr, 0xe0, 0x00);
417 else
418 reg_set_bit_mask(pcf, addr, 0xe0, 0xe0);
420 return 0;
422 EXPORT_SYMBOL_GPL(pcf50606_onoff_set);
424 int pcf50606_onoff_get(struct pcf50606_data *pcf,
425 enum pcf50606_regulator_id reg)
427 u_int8_t val, addr;
429 if (reg >= __NUM_PCF50606_REGULATORS)
430 return -EINVAL;
432 addr = regulator_registers[reg];
433 val = (reg_read(pcf, addr) & 0xe0) >> 5;
435 /* PWREN1 = 1, PWREN2 = 1, see table 16 of datasheet */
436 switch (val) {
437 case 0:
438 case 5:
439 return 0;
440 default:
441 return 1;
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)
450 u_int8_t volt_bits;
451 u_int8_t regnr;
452 int rc;
454 dev_dbg(&pcf->client.dev, "pcf=%p, reg=%d, mvolts=%d\n", pcf, reg,
455 millivolts);
457 if (reg >= __NUM_PCF50606_REGULATORS)
458 return -EINVAL;
460 if (millivolts > pcf->pdata->rails[reg].voltage.max)
461 return -EINVAL;
463 switch (reg) {
464 case PCF50606_REGULATOR_DCD:
465 volt_bits = dcdc_voltage(millivolts);
466 rc = reg_set_bit_mask(pcf, PCF50606_REG_DCDC1, 0x1f,
467 volt_bits);
468 break;
469 case PCF50606_REGULATOR_DCDE:
470 volt_bits = dcdec_voltage(millivolts);
471 rc = reg_set_bit_mask(pcf, PCF50606_REG_DCDEC1, 0x0f,
472 volt_bits);
473 break;
474 case PCF50606_REGULATOR_DCUD:
475 volt_bits = dcudc_voltage(millivolts);
476 rc = reg_set_bit_mask(pcf, PCF50606_REG_DCUDC1, 0x1f,
477 volt_bits);
478 break;
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);
485 break;
486 case PCF50606_REGULATOR_LPREG:
487 volt_bits = dx_voltage(millivolts);
488 rc = reg_set_bit_mask(pcf, PCF50606_REG_LPREGC1, 0x1f,
489 volt_bits);
490 break;
491 case PCF50606_REGULATOR_IOREG:
492 if (millivolts < 1800)
493 return -EINVAL;
494 volt_bits = dx_voltage(millivolts);
495 rc = reg_set_bit_mask(pcf, PCF50606_REG_IOREGC, 0x1f,
496 volt_bits);
497 break;
498 default:
499 return -EINVAL;
502 return rc;
504 EXPORT_SYMBOL_GPL(pcf50606_voltage_set);
506 unsigned int pcf50606_voltage_get(struct pcf50606_data *pcf,
507 enum pcf50606_regulator_id reg)
509 u_int8_t volt_bits;
510 u_int8_t regnr;
511 unsigned int rc = 0;
513 if (reg >= __NUM_PCF50606_REGULATORS)
514 return -EINVAL;
516 switch (reg) {
517 case PCF50606_REGULATOR_DCD:
518 volt_bits = reg_read(pcf, PCF50606_REG_DCDC1) & 0x1f;
519 rc = dcdc_2voltage(volt_bits);
520 break;
521 case PCF50606_REGULATOR_DCDE:
522 volt_bits = reg_read(pcf, PCF50606_REG_DCDEC1) & 0x0f;
523 rc = dcdec_2voltage(volt_bits);
524 break;
525 case PCF50606_REGULATOR_DCUD:
526 volt_bits = reg_read(pcf, PCF50606_REG_DCUDC1) & 0x1f;
527 rc = dcudc_2voltage(volt_bits);
528 break;
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)
535 volt_bits = 0x18;
536 rc = dx_2voltage(volt_bits);
537 break;
538 case PCF50606_REGULATOR_LPREG:
539 volt_bits = reg_read(pcf, PCF50606_REG_LPREGC1) & 0x1f;
540 if (volt_bits > 0x18)
541 volt_bits = 0x18;
542 rc = dx_2voltage(volt_bits);
543 break;
544 case PCF50606_REGULATOR_IOREG:
545 volt_bits = reg_read(pcf, PCF50606_REG_IOREGC) & 0x1f;
546 if (volt_bits > 0x18)
547 volt_bits = 0x18;
548 rc = dx_2voltage(volt_bits);
549 break;
550 default:
551 return -EINVAL;
554 return rc;
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)
568 u_int8_t val;
570 if (on)
571 val = 0x07;
572 else
573 val = 0x0f;
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)
584 return 1;
586 return 0;
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);
594 u_int8_t pcfirq[3];
595 int ret;
597 mutex_lock(&pcf->working_lock);
598 pcf->working = 1;
600 /* sanity */
601 if (!&pcf->client.dev)
602 goto bail;
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))
617 goto bail;
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
626 * completed.
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
630 * completed.
633 if (pcf->suspend_state &&
634 (pcf->suspend_state != PCF50606_SS_COMPLETED_RESUME))
635 goto reschedule;
637 /* this is the case early in resume! Sanity check! */
638 if (i2c_get_clientdata(&pcf->client) == NULL)
639 goto reschedule;
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
646 * while
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.
659 goto reschedule;
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");
677 } else {
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;
697 if (pcf->pdata->cb)
698 pcf->pdata->cb(&pcf->client.dev,
699 PCF50606_FEAT_MBC,
700 PMU_EVT_INSERT);
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) {
714 DEBUGPC("ONKEYF ");
715 input_report_key(pcf->input_dev, KEY_POWER, 1);
716 } else {
717 DEBUGPC("ONKEYF(unreported) ");
720 if (pcfirq[0] & PCF50606_INT1_ONKEY1S) {
721 /* ONKEY pressed for more than 1 second */
722 pcf->onkey_seconds = 0;
723 DEBUGPC("ONKEY1S ");
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) {
736 DEBUGPC("ONKEYR ");
737 input_report_key(pcf->input_dev, KEY_POWER, 0);
738 } else {
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
744 * request it */
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) {
751 DEBUGPC("EXTONR ");
752 input_report_key(pcf->input_dev, KEY_POWER2, 1);
754 if (pcfirq[0] & PCF50606_INT1_EXTONF) {
755 DEBUGPC("EXTONF ");
756 input_report_key(pcf->input_dev, KEY_POWER2, 0);
758 if (pcfirq[0] & PCF50606_INT1_SECOND) {
759 DEBUGPC("SECOND ");
760 if (pcf->flags & PCF50606_F_RTC_SECOND)
761 rtc_update_irq(pcf->rtc, 1,
762 RTC_PF | RTC_IRQF);
764 if (pcf->onkey_seconds >= 0 &&
765 pcf->flags & PCF50606_F_PWR_PRESSED) {
766 DEBUGPC("ONKEY_SECONDS(%u, OOCC1=0x%02x) ",
767 pcf->onkey_seconds,
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) {
793 DEBUGPC("ALARM ");
794 if (pcf->pdata->used_features & PCF50606_FEAT_RTC)
795 rtc_update_irq(pcf->rtc, 1,
796 RTC_AF | RTC_IRQF);
799 if (pcfirq[1] & PCF50606_INT2_CHGINS) {
800 /* Charger inserted */
801 DEBUGPC("CHGINS ");
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;
805 if (pcf->pdata->cb)
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 */
812 DEBUGPC("CHGRM ");
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);
816 if (pcf->pdata->cb)
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 */
823 DEBUGPC("CHGFOK ");
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 */
829 DEBUGPC("CHGERR ");
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 */
836 DEBUGPC("CHGFRDY ");
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 */
843 DEBUGPC("CHGPROT ");
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 */
862 DEBUGPC("ADCRDY ");
864 if (pcfirq[2] & PCF50606_INT3_ACDINS) {
865 /* Accessory insertion detected */
866 DEBUGPC("ACDINS ");
867 if (pcf->pdata->cb)
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 */
873 DEBUGPC("ACDREM ");
874 if (pcf->pdata->cb)
875 pcf->pdata->cb(&pcf->client.dev,
876 PCF50606_FEAT_ACD, PMU_EVT_REMOVE);
878 /* FIXME: TSCPRES */
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
885 DEBUGPC("(NO)BAT ");
886 } else {
887 /* Really low battery voltage, we have 8 seconds left */
888 DEBUGPC("LOWBAT ");
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);
902 } else
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 */
919 DEBUGPC("HIGHTMP ");
920 apm_queue_event(APM_CRITICAL_SUSPEND);
923 DEBUGPC("\n");
925 bail:
926 pcf->working = 0;
927 input_sync(pcf->input_dev);
928 put_device(&pcf->client.dev);
929 mutex_unlock(&pcf->working_lock);
931 return;
933 reschedule:
935 if ((pcf->suspend_state != PCF50606_SS_STARTING_SUSPEND) &&
936 (pcf->suspend_state != PCF50606_SS_COMPLETED_SUSPEND)) {
937 msleep(10);
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",
952 irq, _pcf);
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");
957 return IRQ_HANDLED;
960 static u_int16_t adc_to_batt_millivolts(u_int16_t adc)
962 u_int16_t mvolts;
964 mvolts = (adc * 6000) / 1024;
966 return mvolts;
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;
979 return percent;
982 u_int16_t pcf50606_battvolt(struct pcf50606_data *pcf)
984 u_int16_t adc;
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,
992 char *buf)
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)
1003 int reg_id;
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;
1021 else
1022 reg_id = -1;
1024 return reg_id;
1027 static ssize_t show_vreg(struct device *dev, struct device_attribute *attr,
1028 char *buf)
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);
1035 if (reg_id < 0)
1036 return 0;
1038 if (pcf50606_onoff_get(pcf, reg_id) > 0)
1039 return sprintf(buf, "%u\n", pcf50606_voltage_get(pcf, reg_id));
1040 else
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);
1053 if (reg_id < 0)
1054 return -EIO;
1056 dev_dbg(dev, "attempting to set %s(%d) to %lu mvolts\n",
1057 attr->attr.name, reg_id, mvolts);
1059 if (mvolts == 0) {
1060 pcf50606_onoff_set(pcf, reg_id, 0);
1061 } else {
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);
1066 return -EINVAL;
1068 pcf50606_onoff_set(pcf, reg_id, 1);
1071 return count;
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 /***********************************************************************
1084 * Charger Control
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))
1091 return;
1093 if (on) {
1094 /* We can allow PCF to automatically charge
1095 * using Ifast */
1096 pcf->flags |= PCF50606_F_CHG_FAST;
1097 reg_set_bit_mask(pcf, PCF50606_REG_MBCC1,
1098 PCF50606_MBCC1_AUTOFST,
1099 PCF50606_MBCC1_AUTOFST);
1100 } else {
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
1106 * process */
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)
1117 / (1023 - adc);
1119 return r_ntc;
1122 static inline int16_t rntc_to_temp(u_int16_t rntc)
1124 int i;
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,
1134 char *buf)
1136 struct i2c_client *client = to_i2c_client(dev);
1137 struct pcf50606_data *pcf = i2c_get_clientdata(client);
1138 u_int16_t adc;
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,
1155 char *buf)
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;
1160 int16_t ma;
1162 adc_batvolt = adc_read(pcf, PCF50606_ADCMUX_BATVOLT_ADCIN1,
1163 &adc_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,
1182 char *buf)
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;
1212 else
1213 return -EINVAL;
1215 reg_write(pcf, PCF50606_REG_MBCC1, mbcc1);
1217 return count;
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,
1232 char *buf)
1234 struct i2c_client *client = to_i2c_client(dev);
1235 struct pcf50606_data *pcf = i2c_get_clientdata(client);
1236 char *b = buf;
1237 int i;
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]);
1243 if (b > buf)
1244 b += sprintf(b, "\n");
1246 return b - buf;
1248 static DEVICE_ATTR(chgstate, S_IRUGO | S_IWUSR, show_chgstate, NULL);
1250 /***********************************************************************
1251 * APM emulation
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;
1263 else
1264 info->ac_line_status = APM_AC_OFFLINE;
1266 switch (chgmod) {
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);
1271 break;
1272 default:
1273 info->battery_status = APM_BATTERY_STATUS_CHARGING;
1274 info->battery_flag = APM_BATTERY_FLAG_CHARGING;
1275 break;
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;
1289 int ret = 0;
1291 switch (psp) {
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;
1297 } else {
1299 /* We have a charger present, get charge mode */
1300 mbcc1 = reg_read(pcf, PCF50606_REG_MBCC1);
1301 chgmod = (mbcc1 & PCF50606_MBCC1_CHGMOD_MASK);
1302 switch (chgmod) {
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;
1310 break;
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;
1318 break;
1320 default:
1321 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
1322 break;
1327 case POWER_SUPPLY_PROP_PRESENT:
1328 val->intval = 1; /* Must be, or the magic smoke comes out */
1329 break;
1331 case POWER_SUPPLY_PROP_ONLINE:
1332 val->intval = !!(reg_read(pcf, PCF50606_REG_OOCS) &
1333 PCF50606_OOCS_EXTON);
1334 break;
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;
1340 break;
1342 case POWER_SUPPLY_PROP_CURRENT_NOW:
1343 adc = adc_read(pcf, PCF50606_ADCMUX_BATVOLT_ADCIN1,
1344 &adc_adcin1);
1345 val->intval = adc_to_chg_milliamps(pcf, adc_adcin1, adc) * 1000;
1346 break;
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;
1351 break;
1353 case POWER_SUPPLY_PROP_CAPACITY:
1354 val->intval = battvolt_scale(pcf50606_battvolt(pcf));
1355 break;
1357 default:
1358 ret = -EINVAL;
1359 break;
1362 return ret;
1365 /***********************************************************************
1366 * RTC
1367 ***********************************************************************/
1369 struct pcf50606_time {
1370 u_int8_t sec;
1371 u_int8_t min;
1372 u_int8_t hour;
1373 u_int8_t wkday;
1374 u_int8_t day;
1375 u_int8_t month;
1376 u_int8_t year;
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,
1402 unsigned long arg)
1404 struct i2c_client *client = to_i2c_client(dev);
1405 struct pcf50606_data *pcf = i2c_get_clientdata(client);
1407 switch (cmd) {
1408 case RTC_AIE_OFF:
1409 /* disable the alarm interrupt */
1410 reg_set_bit_mask(pcf, PCF50606_REG_INT1M,
1411 PCF50606_INT1_ALARM, PCF50606_INT1_ALARM);
1412 return 0;
1413 case RTC_AIE_ON:
1414 /* enable the alarm interrupt */
1415 reg_clear_bits(pcf, PCF50606_REG_INT1M, PCF50606_INT1_ALARM);
1416 return 0;
1417 case RTC_PIE_OFF:
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);
1422 return 0;
1423 case RTC_PIE_ON:
1424 /* ensable periodic interrupt (hz tick) */
1425 pcf->flags |= PCF50606_F_RTC_SECOND;
1426 reg_clear_bits(pcf, PCF50606_REG_INT1M, PCF50606_INT1_SECOND);
1427 return 0;
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);
1458 return 0;
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;
1466 u_int8_t int1m;
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);
1495 return 0;
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);
1505 alrm->enabled =
1506 __reg_read(pcf, PCF50606_REG_INT1M) & PCF50606_INT1_ALARM
1507 ? 0 : 1;
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);
1519 return 0;
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;
1527 u_int8_t irqmask;
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);
1555 /* FIXME */
1556 return 0;
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 /***********************************************************************
1568 * Watchdog
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);
1605 else {
1606 printk(KERN_CRIT "Unexpected close, not stopping watchdog!\n");
1607 pcf50606_wdt_keepalive(pcf);
1610 pcf->allow_close = CLOSE_STATE_NOT;
1612 return 0;
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;
1619 if (len) {
1620 size_t i;
1622 for (i = 0; i != len; i++) {
1623 char c;
1624 if (get_user(c, data + i))
1625 return -EFAULT;
1626 if (c == 'V')
1627 pcf->allow_close = CLOSE_STATE_ALLOW;
1629 pcf50606_wdt_keepalive(pcf);
1632 return len;
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;
1648 switch (cmd) {
1649 case WDIOC_GETSUPPORT:
1650 return copy_to_user(argp, &pcf50606_wdt_ident,
1651 sizeof(pcf50606_wdt_ident)) ? -EFAULT : 0;
1652 break;
1653 case WDIOC_GETSTATUS:
1654 case WDIOC_GETBOOTSTATUS:
1655 return put_user(0, p);
1656 case WDIOC_KEEPALIVE:
1657 pcf50606_wdt_keepalive(pcf);
1658 return 0;
1659 case WDIOC_GETTIMEOUT:
1660 return put_user(8, p);
1661 default:
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,
1677 .name = "watchdog",
1678 .fops = &pcf50606_wdt_fops,
1681 /***********************************************************************
1682 * PWM
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,
1693 char *buf)
1695 struct i2c_client *client = to_i2c_client(dev);
1696 struct pcf50606_data *pcf = i2c_get_clientdata(client);
1697 u_int8_t val;
1699 val = reg_read(pcf, PCF50606_REG_PWMC1) >> PCF50606_PWMC1_DC_SHIFT;
1700 val &= 0xf;
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);
1710 u_int8_t i;
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));
1719 return count;
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,
1730 char *buf)
1732 struct i2c_client *client = to_i2c_client(dev);
1733 struct pcf50606_data *pcf = i2c_get_clientdata(client);
1734 u_int8_t val;
1736 val = reg_read(pcf, PCF50606_REG_PWMC1) >> PCF50606_PWMC1_CLK_SHIFT;
1737 val &= 0x7;
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);
1747 u_int8_t i;
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",
1752 pwm_clk_table[i]);
1753 reg_set_bit_mask(pcf, PCF50606_REG_PWMC1, 0xe0,
1754 (i << PCF50606_PWMC1_CLK_SHIFT));
1757 return count;
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)
1777 intensity = 0;
1778 if (bd->props.fb_blank != FB_BLANK_UNBLANK)
1779 intensity = 0;
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",
1804 #endif
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,
1815 NULL
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)
1825 int i = 0;
1826 struct attribute **attr;
1828 for (attr = pcf_sysfs_entries; *attr; attr++)
1829 i++;
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;
1855 int err = 0;
1856 int irq;
1858 if (!pcf50606_pdev) {
1859 printk(KERN_ERR "pcf50606: driver needs a platform_device!\n");
1860 return -EIO;
1863 irq = platform_get_irq(pcf50606_pdev, 0);
1864 if (irq < 0) {
1865 dev_err(&pcf50606_pdev->dev, "no irq in platform resources!\n");
1866 return -EIO;
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");
1873 return -EBUSY;
1876 data = kzalloc(sizeof(*data), GFP_KERNEL);
1877 if (!data)
1878 return -ENOMEM;
1880 mutex_init(&data->lock);
1881 mutex_init(&data->working_lock);
1882 INIT_WORK(&data->work, pcf50606_work);
1883 data->irq = irq;
1884 data->working = 0;
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);
1901 if (err) {
1902 dev_err(&new_client->dev,
1903 "error during i2c_attach_client()\n");
1904 goto exit_free;
1907 populate_sysfs_group(data);
1909 err = sysfs_create_group(&new_client->dev.kobj, &pcf_attr_group);
1910 if (err) {
1911 dev_err(&new_client->dev, "error creating sysfs group\n");
1912 goto exit_detach;
1915 /* create virtual charger 'device' */
1917 /* input device registration */
1918 data->input_dev = input_allocate_device();
1919 if (!data->input_dev)
1920 goto exit_sysfs;
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);
1933 if (err)
1934 goto exit_sysfs;
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,
1946 "pcf50606", data);
1947 if (err < 0)
1948 goto exit_input;
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);
1961 goto exit_irq;
1965 if (data->pdata->used_features & PCF50606_FEAT_WDT) {
1966 err = misc_register(&pcf50606_wdt_miscdev);
1967 if (err) {
1968 dev_err(&new_client->dev, "cannot register miscdev on "
1969 "minor=%d (%d)\n", WATCHDOG_MINOR, err);
1970 goto exit_rtc;
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",
1983 &new_client->dev,
1984 data,
1985 &pcf50606bl_ops);
1986 if (!data->backlight)
1987 goto exit_misc;
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(&gta01_pm_bt_dev);
2007 break;
2009 platform_device_register(&gta01_pm_gps_dev);
2010 /* a link for gllin compatibility */
2011 err = sysfs_create_link(&platform_bus_type.devices.kobj,
2012 &gta01_pm_gps_dev.dev.kobj, "gta01-pm-gps.0");
2013 if (err)
2014 printk(KERN_ERR
2015 "sysfs_create_link (gta01-pm-gps.0): %d\n", err);
2017 #endif
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);
2022 else
2023 reg_clear_bits(data, PCF50606_REG_ACDC1,
2024 PCF50606_ACDC1_ACDAPE);
2026 return 0;
2028 exit_misc:
2029 if (data->pdata->used_features & PCF50606_FEAT_WDT)
2030 misc_deregister(&pcf50606_wdt_miscdev);
2031 exit_rtc:
2032 if (data->pdata->used_features & PCF50606_FEAT_RTC)
2033 rtc_device_unregister(pcf50606_global->rtc);
2034 exit_irq:
2035 free_irq(pcf50606_global->irq, pcf50606_global);
2036 pcf50606_global = NULL;
2037 exit_input:
2038 pm_power_off = NULL;
2039 input_unregister_device(data->input_dev);
2040 exit_sysfs:
2041 sysfs_remove_group(&new_client->dev.kobj, &pcf_attr_group);
2042 exit_detach:
2043 i2c_detach_client(new_client);
2044 exit_free:
2045 kfree(data);
2046 return err;
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;
2078 kfree(pcf);
2080 return 0;
2083 #ifdef CONFIG_PM
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);
2097 int i;
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))
2103 return -EBUSY;
2105 /* The general idea is to power down all unused power supplies,
2106 * and then mask all PCF50606 interrup sources but EXTONR, ONKEYF
2107 * and ALARM */
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)) {
2137 u_int8_t tmp;
2139 /* IOREG powers the I@C interface so we cannot switch
2140 * it off */
2141 if (i == PCF50606_REGULATOR_IOREG)
2142 continue;
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]);
2148 tmp &= 0x1f;
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);
2164 return 0;
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);
2206 return 0;
2208 #else
2209 #define pcf50606_suspend NULL
2210 #define pcf50606_resume NULL
2211 #endif
2213 static struct i2c_driver pcf50606_driver = {
2214 .driver = {
2215 .name = "pcf50606",
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;
2229 if (!pdata)
2230 return -ENODEV;
2232 pcf50606_pdev = pdev;
2234 return 0;
2237 static int pcf50606_plat_remove(struct platform_device *pdev)
2239 return 0;
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;
2254 return 0;
2257 static struct platform_driver pcf50606_plat_driver = {
2258 .probe = pcf50606_plat_probe,
2259 .remove = pcf50606_plat_remove,
2260 .resume_early = pcf50606_plat_resume,
2261 .driver = {
2262 .owner = THIS_MODULE,
2263 .name = "pcf50606",
2267 static int __init pcf50606_init(void)
2269 int rc;
2271 rc = platform_driver_register(&pcf50606_plat_driver);
2272 if (!rc)
2273 rc = i2c_add_driver(&pcf50606_driver);
2275 return rc;
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);