2c739b41fe14ce48f77167f9486bde6fef66774a
[kernel.git] / drivers / i2c / chips / pcf50633.c
1 /* Philips PCF50633 Power Management Unit (PMU) driver
2  *
3  * (C) 2006-2007 by Openmoko, Inc.
4  * Author: Harald Welte <laforge@openmoko.org>
5  * All rights reserved.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of
10  * the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20  * MA 02111-1307 USA
21  *
22  * This driver is a monster ;) It provides the following features
23  * - voltage control for a dozen different voltage domains
24  * - charging control for main and backup battery
25  * - rtc / alarm
26  * - adc driver (hw_sensors like)
27  * - backlight
28  *
29  */
30
31 #include <linux/module.h>
32 #include <linux/init.h>
33 #include <linux/i2c.h>
34 #include <linux/types.h>
35 #include <linux/interrupt.h>
36 #include <linux/irq.h>
37 #include <linux/workqueue.h>
38 #include <linux/delay.h>
39 #include <linux/rtc.h>
40 #include <linux/bcd.h>
41 #include <linux/watchdog.h>
42 #include <linux/miscdevice.h>
43 #include <linux/input.h>
44 #include <linux/fb.h>
45 #include <linux/backlight.h>
46 #include <linux/sched.h>
47 #include <linux/platform_device.h>
48 #include <linux/pcf50633.h>
49 #include <linux/apm-emulation.h>
50 #include <linux/jiffies.h>
51
52 #include <asm/mach-types.h>
53
54 #include "pcf50633.h"
55 #include <linux/resume-dependency.h>
56
57 #if 1
58 #define DEBUGP(x, args ...) printk("%s: " x, __FUNCTION__, ## args)
59 #define DEBUGPC(x, args ...) printk(x, ## args)
60 #else
61 #define DEBUGP(x, args ...)
62 #define DEBUGPC(x, args ...)
63 #endif
64
65 /***********************************************************************
66  * Static data / structures
67  ***********************************************************************/
68
69 static unsigned short normal_i2c[] = { 0x73, I2C_CLIENT_END };
70
71 I2C_CLIENT_INSMOD_1(pcf50633);
72
73 #define PCF50633_FIDX_CHG_ENABLED       0       /* Charger enabled */
74 #define PCF50633_FIDX_CHG_PRESENT       1       /* Charger present */
75 #define PCF50633_FIDX_CHG_ERR           3       /* Charger Error */
76 #define PCF50633_FIDX_CHG_PROT          4       /* Charger Protection */
77 #define PCF50633_FIDX_CHG_READY         5       /* Charging completed */
78 #define PCF50633_FIDX_PWR_PRESSED       8
79 #define PCF50633_FIDX_RTC_SECOND        9
80 #define PCF50633_FIDX_USB_PRESENT       10
81
82 #define PCF50633_F_CHG_ENABLED  (1 << PCF50633_FIDX_CHG_ENABLED)
83 #define PCF50633_F_CHG_PRESENT  (1 << PCF50633_FIDX_CHG_PRESENT)
84 #define PCF50633_F_CHG_ERR      (1 << PCF50633_FIDX_CHG_ERR)
85 #define PCF50633_F_CHG_PROT     (1 << PCF50633_FIDX_CHG_PROT)
86 #define PCF50633_F_CHG_READY    (1 << PCF50633_FIDX_CHG_READY)
87
88 #define PCF50633_F_CHG_MASK     0x000000fc
89
90 #define PCF50633_F_PWR_PRESSED  (1 << PCF50633_FIDX_PWR_PRESSED)
91 #define PCF50633_F_RTC_SECOND   (1 << PCF50633_FIDX_RTC_SECOND)
92 #define PCF50633_F_USB_PRESENT  (1 << PCF50633_FIDX_USB_PRESENT)
93
94 enum close_state {
95         CLOSE_STATE_NOT,
96         CLOSE_STATE_ALLOW = 0x2342,
97 };
98
99 enum charger_type {
100         CHARGER_TYPE_NONE = 0,
101         CHARGER_TYPE_HOSTUSB,
102         CHARGER_TYPE_1A
103 };
104
105 #define ADC_NOM_CHG_DETECT_1A 6
106 #define ADC_NOM_CHG_DETECT_NONE 43
107
108 #define MAX_ADC_FIFO_DEPTH 8
109
110 enum pcf50633_suspend_states {
111         PCF50633_SS_RUNNING,
112         PCF50633_SS_STARTING_SUSPEND,
113         PCF50633_SS_COMPLETED_SUSPEND,
114         PCF50633_SS_RESUMING_BUT_NOT_US_YET,
115         PCF50633_SS_STARTING_RESUME,
116         PCF50633_SS_COMPLETED_RESUME,
117 };
118
119
120 struct pcf50633_data {
121         struct i2c_client client;
122         struct pcf50633_platform_data *pdata;
123         struct backlight_device *backlight;
124         struct mutex lock;
125         unsigned int flags;
126         unsigned int working;
127         struct mutex working_lock;
128         struct work_struct work;
129         struct rtc_device *rtc;
130         struct input_dev *input_dev;
131         int allow_close;
132         int onkey_seconds;
133         int irq;
134         enum pcf50633_suspend_states suspend_state;
135         int usb_removal_count;
136         u8 pcfirq_resume[5];
137         int probe_completed;
138
139         /* if he pulls battery while charging, we notice that and correctly
140          * report that the charger is idle.  But there is no interrupt that
141          * fires if he puts a battery back in and charging resumes.  So when
142          * the battery is pulled, we run this work function looking for
143          * either charger resumption or USB cable pull
144          */
145         struct mutex working_lock_nobat;
146         struct work_struct work_nobat;
147         int working_nobat;
148         int usb_removal_count_nobat;
149         int jiffies_last_bat_ins;
150
151         /* current limit notification handler stuff */
152         struct mutex working_lock_usb_curlimit;
153         struct work_struct work_usb_curlimit;
154         int pending_curlimit;
155         int usb_removal_count_usb_curlimit;
156
157         int last_curlim_set;
158
159         int coldplug_done; /* cleared by probe, set by first work service */
160         int flag_bat_voltage_read; /* ipc to /sys batt voltage read func */
161
162         int charger_adc_result_raw;
163         enum charger_type charger_type;
164
165         /* we have a FIFO of ADC measurement requests that are used only by
166          * the workqueue service code after the ADC completion interrupt
167          */
168         int adc_queue_mux[MAX_ADC_FIFO_DEPTH]; /* which ADC input to use */
169         int adc_queue_avg[MAX_ADC_FIFO_DEPTH]; /* amount of averaging */
170         int adc_queue_head; /* head owned by foreground code */
171         int adc_queue_tail; /* tail owned by service code */
172
173 #ifdef CONFIG_PM
174         struct {
175                 u_int8_t ooctim2;
176                 /* enables are always [1] below
177                  * I2C has limit of 32 sequential regs, so done in two lumps
178                  * because it covers 33 register extent otherwise
179                  */
180                 u_int8_t misc[PCF50633_REG_LEDDIM - PCF50633_REG_AUTOOUT + 1];
181                 /*  skip 1 reserved reg here */
182                 u_int8_t ldo[PCF50633_REG_HCLDOENA - PCF50633_REG_LDO1OUT + 1];
183         } standby_regs;
184
185         struct resume_dependency resume_dependency;
186
187 #endif
188 };
189
190 static struct i2c_driver pcf50633_driver;
191
192 struct pcf50633_data *pcf50633_global;
193 EXPORT_SYMBOL_GPL(pcf50633_global);
194
195 static struct platform_device *pcf50633_pdev;
196
197 static void pcf50633_usb_curlim_set(struct pcf50633_data *pcf, int ma);
198 static void pcf50633_charge_enable(struct pcf50633_data *pcf, int on);
199
200
201 /***********************************************************************
202  * Low-Level routines
203  ***********************************************************************/
204
205 static int __reg_write(struct pcf50633_data *pcf, u_int8_t reg, u_int8_t val)
206 {
207         if (pcf->suspend_state == PCF50633_SS_COMPLETED_SUSPEND) {
208                 dev_err(&pcf->client.dev, "__reg_write while suspended\n");
209                 dump_stack();
210         }
211         return i2c_smbus_write_byte_data(&pcf->client, reg, val);
212 }
213
214 static int reg_write(struct pcf50633_data *pcf, u_int8_t reg, u_int8_t val)
215 {
216         int ret;
217
218         mutex_lock(&pcf->lock);
219         ret = __reg_write(pcf, reg, val);
220         mutex_unlock(&pcf->lock);
221
222         return ret;
223 }
224
225 static int32_t __reg_read(struct pcf50633_data *pcf, u_int8_t reg)
226 {
227         int32_t ret;
228
229         if (pcf->suspend_state == PCF50633_SS_COMPLETED_SUSPEND) {
230                 dev_err(&pcf->client.dev, "__reg_read while suspended\n");
231                 dump_stack();
232         }
233         ret = i2c_smbus_read_byte_data(&pcf->client, reg);
234
235         return ret;
236 }
237
238 static u_int8_t reg_read(struct pcf50633_data *pcf, u_int8_t reg)
239 {
240         int32_t ret;
241
242         mutex_lock(&pcf->lock);
243         ret = __reg_read(pcf, reg);
244         mutex_unlock(&pcf->lock);
245
246         return ret & 0xff;
247 }
248
249 static int reg_set_bit_mask(struct pcf50633_data *pcf,
250                             u_int8_t reg, u_int8_t mask, u_int8_t val)
251 {
252         int ret;
253         u_int8_t tmp;
254
255         val &= mask;
256
257         mutex_lock(&pcf->lock);
258
259         tmp = __reg_read(pcf, reg);
260         tmp &= ~mask;
261         tmp |= val;
262         ret = __reg_write(pcf, reg, tmp);
263
264         mutex_unlock(&pcf->lock);
265
266         return ret;
267 }
268
269 static int reg_clear_bits(struct pcf50633_data *pcf, u_int8_t reg, u_int8_t val)
270 {
271         int ret;
272         u_int8_t tmp;
273
274         mutex_lock(&pcf->lock);
275
276         tmp = __reg_read(pcf, reg);
277         tmp &= ~val;
278         ret = __reg_write(pcf, reg, tmp);
279
280         mutex_unlock(&pcf->lock);
281
282         return ret;
283 }
284
285 /* asynchronously setup reading one ADC channel */
286 static void async_adc_read_setup(struct pcf50633_data *pcf,
287                                  int channel, int avg)
288 {
289         channel &= PCF50633_ADCC1_ADCMUX_MASK;
290
291         /* kill ratiometric, but enable ACCSW biasing */
292         __reg_write(pcf, PCF50633_REG_ADCC2, 0x00);
293         __reg_write(pcf, PCF50633_REG_ADCC3, 0x01);
294
295         /* start ADC conversion of selected channel */
296         __reg_write(pcf, PCF50633_REG_ADCC1, channel | avg |
297                     PCF50633_ADCC1_ADCSTART | PCF50633_ADCC1_RES_10BIT);
298
299 }
300
301 static u_int16_t async_adc_complete(struct pcf50633_data *pcf)
302 {
303         u_int16_t ret = (__reg_read(pcf, PCF50633_REG_ADCS1) << 2) |
304                         (__reg_read(pcf, PCF50633_REG_ADCS3) &
305                                                   PCF50633_ADCS3_ADCDAT1L_MASK);
306
307         DEBUGPC("adc result = %d\n", ret);
308
309         return ret;
310 }
311
312
313
314
315 /***********************************************************************
316  * Voltage / ADC
317  ***********************************************************************/
318
319 static u_int8_t auto_voltage(unsigned int millivolts)
320 {
321         if (millivolts < 1800)
322                 return 0;
323         if (millivolts > 3800)
324                 return 0xff;
325
326         millivolts -= 625;
327         return millivolts/25;
328 }
329
330 static unsigned int auto_2voltage(u_int8_t bits)
331 {
332         if (bits < 0x2f)
333                 return 0;
334         return 625 + (bits * 25);
335 }
336
337 static u_int8_t down_voltage(unsigned int millivolts)
338 {
339         if (millivolts < 625)
340                 return 0;
341         else if (millivolts > 3000)
342                 return 0xff;
343
344         millivolts -= 625;
345         return millivolts/25;
346 }
347
348 static unsigned int down_2voltage(u_int8_t bits)
349 {
350         return 625 + (bits*25);
351 }
352
353 static u_int8_t ldo_voltage(unsigned int millivolts)
354 {
355         if (millivolts < 900)
356                 return 0;
357         else if (millivolts > 3600)
358                 return 0x1f;
359
360         millivolts -= 900;
361         return millivolts/100;
362 }
363
364 static unsigned int ldo_2voltage(u_int8_t bits)
365 {
366         bits &= 0x1f;
367         return 900 + (bits * 100);
368 }
369
370 static const u_int8_t regulator_registers[__NUM_PCF50633_REGULATORS] = {
371         [PCF50633_REGULATOR_AUTO]       = PCF50633_REG_AUTOOUT,
372         [PCF50633_REGULATOR_DOWN1]      = PCF50633_REG_DOWN1OUT,
373         [PCF50633_REGULATOR_DOWN2]      = PCF50633_REG_DOWN2OUT,
374         [PCF50633_REGULATOR_MEMLDO]     = PCF50633_REG_MEMLDOOUT,
375         [PCF50633_REGULATOR_LDO1]       = PCF50633_REG_LDO1OUT,
376         [PCF50633_REGULATOR_LDO2]       = PCF50633_REG_LDO2OUT,
377         [PCF50633_REGULATOR_LDO3]       = PCF50633_REG_LDO3OUT,
378         [PCF50633_REGULATOR_LDO4]       = PCF50633_REG_LDO4OUT,
379         [PCF50633_REGULATOR_LDO5]       = PCF50633_REG_LDO5OUT,
380         [PCF50633_REGULATOR_LDO6]       = PCF50633_REG_LDO6OUT,
381         [PCF50633_REGULATOR_HCLDO]      = PCF50633_REG_HCLDOOUT,
382 };
383
384 int pcf50633_onoff_set(struct pcf50633_data *pcf,
385                        enum pcf50633_regulator_id reg, int on)
386 {
387         u_int8_t addr;
388
389         if (reg >= __NUM_PCF50633_REGULATORS)
390                 return -EINVAL;
391
392         /* the *ENA register is always one after the *OUT register */
393         addr = regulator_registers[reg] + 1;
394
395         if (on == 0)
396                 reg_set_bit_mask(pcf, addr, PCF50633_REGULATOR_ON, 0);
397         else
398                 reg_set_bit_mask(pcf, addr, PCF50633_REGULATOR_ON,
399                                  PCF50633_REGULATOR_ON);
400
401         return 0;
402 }
403 EXPORT_SYMBOL_GPL(pcf50633_onoff_set);
404
405 int pcf50633_onoff_get(struct pcf50633_data *pcf,
406                        enum pcf50633_regulator_id reg)
407 {
408         u_int8_t val, addr;
409
410         if (reg >= __NUM_PCF50633_REGULATORS)
411                 return -EINVAL;
412
413         /* the *ENA register is always one after the *OUT register */
414         addr = regulator_registers[reg] + 1;
415         val = reg_read(pcf, addr) & PCF50633_REGULATOR_ON;
416
417         return val;
418 }
419 EXPORT_SYMBOL_GPL(pcf50633_onoff_get);
420
421 int pcf50633_voltage_set(struct pcf50633_data *pcf,
422                          enum pcf50633_regulator_id reg,
423                          unsigned int millivolts)
424 {
425         u_int8_t volt_bits;
426         u_int8_t regnr;
427
428         DEBUGP("pcf=%p, reg=%d, mvolts=%d\n", pcf, reg, millivolts);
429
430         if (reg >= __NUM_PCF50633_REGULATORS)
431                 return -EINVAL;
432
433         regnr = regulator_registers[reg];
434
435         if (millivolts > pcf->pdata->rails[reg].voltage.max)
436                 return -EINVAL;
437
438         switch (reg) {
439         case PCF50633_REGULATOR_AUTO:
440                 volt_bits = auto_voltage(millivolts);
441                 break;
442         case PCF50633_REGULATOR_DOWN1:
443                 volt_bits = down_voltage(millivolts);
444                 break;
445         case PCF50633_REGULATOR_DOWN2:
446                 volt_bits = down_voltage(millivolts);
447                 break;
448         case PCF50633_REGULATOR_LDO1:
449         case PCF50633_REGULATOR_LDO2:
450         case PCF50633_REGULATOR_LDO3:
451         case PCF50633_REGULATOR_LDO4:
452         case PCF50633_REGULATOR_LDO5:
453         case PCF50633_REGULATOR_LDO6:
454         case PCF50633_REGULATOR_HCLDO:
455                 volt_bits = ldo_voltage(millivolts);
456                 DEBUGP("ldo_voltage(0x%x)=%u\n", millivolts, volt_bits);
457                 break;
458         default:
459                 return -EINVAL;
460         }
461
462         return reg_write(pcf, regnr, volt_bits);
463 }
464 EXPORT_SYMBOL_GPL(pcf50633_voltage_set);
465
466 unsigned int pcf50633_voltage_get(struct pcf50633_data *pcf,
467                          enum pcf50633_regulator_id reg)
468 {
469         u_int8_t volt_bits;
470         u_int8_t regnr;
471         unsigned int rc = 0;
472
473         if (reg >= __NUM_PCF50633_REGULATORS)
474                 return -EINVAL;
475
476         regnr = regulator_registers[reg];
477         volt_bits = reg_read(pcf, regnr);
478
479         switch (reg) {
480         case PCF50633_REGULATOR_AUTO:
481                 rc = auto_2voltage(volt_bits);
482                 break;
483         case PCF50633_REGULATOR_DOWN1:
484                 rc = down_2voltage(volt_bits);
485                 break;
486         case PCF50633_REGULATOR_DOWN2:
487                 rc = down_2voltage(volt_bits);
488                 break;
489         case PCF50633_REGULATOR_LDO1:
490         case PCF50633_REGULATOR_LDO2:
491         case PCF50633_REGULATOR_LDO3:
492         case PCF50633_REGULATOR_LDO4:
493         case PCF50633_REGULATOR_LDO5:
494         case PCF50633_REGULATOR_LDO6:
495         case PCF50633_REGULATOR_HCLDO:
496                 rc = ldo_2voltage(volt_bits);
497                 break;
498         default:
499                 return -EINVAL;
500         }
501
502         return rc;
503 }
504 EXPORT_SYMBOL_GPL(pcf50633_voltage_get);
505
506 /* go into 'STANDBY' mode, i.e. power off the main CPU and peripherals */
507 void pcf50633_go_standby(void)
508 {
509         reg_set_bit_mask(pcf50633_global, PCF50633_REG_OOCSHDWN,
510                   PCF50633_OOCSHDWN_GOSTDBY, PCF50633_OOCSHDWN_GOSTDBY);
511 }
512 EXPORT_SYMBOL_GPL(pcf50633_go_standby);
513
514 void pcf50633_gpio_set(struct pcf50633_data *pcf, enum pcf50633_gpio gpio,
515                         int on)
516 {
517         u_int8_t reg = gpio - PCF50633_GPIO1 + PCF50633_REG_GPIO1CFG;
518
519         if (on)
520                 reg_set_bit_mask(pcf, reg, 0x0f, 0x07);
521         else
522                 reg_set_bit_mask(pcf, reg, 0x0f, 0x00);
523 }
524 EXPORT_SYMBOL_GPL(pcf50633_gpio_set);
525
526 int pcf50633_gpio_get(struct pcf50633_data *pcf, enum pcf50633_gpio gpio)
527 {
528         u_int8_t reg = gpio - PCF50633_GPIO1 + PCF50633_REG_GPIO1CFG;
529         u_int8_t val = reg_read(pcf, reg) & 0x0f;
530
531         if (val == PCF50633_GPOCFG_GPOSEL_1 ||
532             val == (PCF50633_GPOCFG_GPOSEL_0|PCF50633_GPOCFG_GPOSEL_INVERSE))
533                 return 1;
534
535         return 0;
536 }
537 EXPORT_SYMBOL_GPL(pcf50633_gpio_get);
538
539 static int interpret_charger_type_from_adc(struct pcf50633_data *pcf,
540                                            int sample)
541 {
542         /* 1A capable charger? */
543
544         if (sample < ((ADC_NOM_CHG_DETECT_NONE + ADC_NOM_CHG_DETECT_1A) / 2))
545                 return CHARGER_TYPE_1A;
546
547         /* well then, nothing in the USB hole, or USB host / unk adapter */
548
549         if (pcf->flags & PCF50633_F_USB_PRESENT) /* ooh power is in there */
550                 return CHARGER_TYPE_HOSTUSB; /* HOSTUSB is the catchall */
551
552         return CHARGER_TYPE_NONE; /* no really -- nothing in there */
553 }
554
555
556
557 static void configure_pmu_for_charger(struct pcf50633_data *pcf,
558                                       enum charger_type type)
559 {
560         switch (type) {
561         case CHARGER_TYPE_NONE:
562                 pcf50633_usb_curlim_set(pcf, 0);
563                 break;
564         /*
565          * the PCF50633 has a feature that it will supply only excess current
566          * from the charger that is not used to power the device.  So this
567          * 500mA setting is "up to 500mA" according to that.
568          */
569         case CHARGER_TYPE_HOSTUSB:
570                 /* USB subsystem should call pcf50633_usb_curlim_set to set
571                  * what was negotiated with the host when it is enumerated
572                  * successfully.  If we get called again after a good
573                  * negotiation, we keep what was negotiated.  (Removal of
574                  * USB plug destroys pcf->last_curlim_set to 0)
575                  */
576                 if (pcf->last_curlim_set > 100)
577                         pcf50633_usb_curlim_set(pcf, pcf->last_curlim_set);
578                 else
579                         pcf50633_usb_curlim_set(pcf, 100);
580                 break;
581         case CHARGER_TYPE_1A:
582                 pcf50633_usb_curlim_set(pcf, 1000);
583                 /*
584                  * stop GPO / EN_HOSTUSB power driving out on the same
585                  * USB power pins we have a 1A charger on right now!
586                  */
587                 dev_dbg(&pcf->client.dev, "Charger -> CHARGER_TYPE_1A\n");
588                 __reg_write(pcf, PCF50633_GPO - PCF50633_GPIO1 +
589                                  PCF50633_REG_GPIO1CFG,
590                                  __reg_read(pcf, PCF50633_GPO - PCF50633_GPIO1 +
591                                                  PCF50633_REG_GPIO1CFG) & 0xf0);
592                 break;
593         }
594
595         /* max out USB fast charge current -- actual current drawn is
596          * additionally limited by USB limit so no worries
597          */
598         __reg_write(pcf, PCF50633_REG_MBCC5, 0xff);
599
600 }
601
602 static void trigger_next_adc_job_if_any(struct pcf50633_data *pcf)
603 {
604         if (pcf->adc_queue_head == pcf->adc_queue_tail)
605                 return;
606         async_adc_read_setup(pcf,
607                              pcf->adc_queue_mux[pcf->adc_queue_tail],
608                              pcf->adc_queue_avg[pcf->adc_queue_tail]);
609 }
610
611 static void add_request_to_adc_queue(struct pcf50633_data *pcf,
612                                      int mux, int avg)
613 {
614         int old_head = pcf->adc_queue_head;
615         pcf->adc_queue_mux[pcf->adc_queue_head] = mux;
616         pcf->adc_queue_avg[pcf->adc_queue_head] = avg;
617
618         pcf->adc_queue_head = (pcf->adc_queue_head + 1) &
619                               (MAX_ADC_FIFO_DEPTH - 1);
620
621         /* it was idle before we just added this?  we need to kick it then */
622         if (old_head == pcf->adc_queue_tail)
623                 trigger_next_adc_job_if_any(pcf);
624 }
625
626 /*
627  * we get run to handle servicing the async notification from USB stack that
628  * we got enumerated and allowed to draw a particular amount of current
629  */
630
631 static void pcf50633_work_usbcurlim(struct work_struct *work)
632 {
633         struct pcf50633_data *pcf =
634                     container_of(work, struct pcf50633_data, work_usb_curlimit);
635
636         mutex_lock(&pcf->working_lock_usb_curlimit);
637
638         /* just can't cope with it if we are suspending, don't reschedule */
639         if ((pcf->suspend_state == PCF50633_SS_STARTING_SUSPEND) ||
640             (pcf->suspend_state == PCF50633_SS_COMPLETED_SUSPEND))
641                 goto bail;
642
643         dev_dbg(&pcf->client.dev, "pcf50633_work_usbcurlim\n");
644
645         if (!pcf->probe_completed)
646                 goto reschedule;
647
648         /* we got a notification from USB stack before we completed resume...
649          * that can only make trouble, reschedule for a retry
650          */
651         if (pcf->suspend_state &&
652                      (pcf->suspend_state < PCF50633_SS_COMPLETED_RESUME))
653                 goto reschedule;
654
655         /*
656          * did he pull USB before we managed to set the limit?
657          */
658         if (pcf->usb_removal_count_usb_curlimit != pcf->usb_removal_count)
659                 goto bail;
660
661         /* OK let's set the requested limit and finish */
662
663         dev_dbg(&pcf->client.dev, "pcf50633_work_usbcurlim setting %dmA\n",
664                                                          pcf->pending_curlimit);
665         pcf50633_usb_curlim_set(pcf, pcf->pending_curlimit);
666
667 bail:
668         mutex_unlock(&pcf->working_lock_usb_curlimit);
669         return;
670
671 reschedule:
672         dev_dbg(&pcf->client.dev, "pcf50633_work_usbcurlim rescheduling\n");
673         if (!schedule_work(&pcf->work_usb_curlimit))
674                 dev_err(&pcf->client.dev, "curlim reschedule work "
675                                                             "already queued\n");
676
677         mutex_unlock(&pcf->working_lock_usb_curlimit);
678         /* don't spew, delaying whatever else is happening */
679         msleep(1);
680 }
681
682
683 /* this is an export to allow machine to set USB current limit according to
684  * notifications of USB stack about enumeration state.  We spawn a work
685  * function to handle the actual setting, because suspend / resume and such
686  * can be in a bad state since this gets called externally asychronous to
687  * anything else going on in pcf50633.
688  */
689
690 int pcf50633_notify_usb_current_limit_change(struct pcf50633_data *pcf,
691                                                                 unsigned int ma)
692 {
693         /* can happen if he calls with pcf50633_global before probe
694          * have to bail with error since we can't even schedule the work
695          */
696         if (!pcf) {
697                 printk(KERN_ERR "pcf50633_notify_usb_current_limit called with NULL pcf\n");
698                 return -EBUSY;
699         }
700
701         dev_dbg(&pcf->client.dev,
702                  "pcf50633_notify_usb_current_limit_change %dmA\n", ma);
703
704         /* prepare to detect USB power removal before we complete */
705         pcf->usb_removal_count_usb_curlimit = pcf->usb_removal_count;
706
707         pcf->pending_curlimit = ma;
708
709         if (!schedule_work(&pcf->work_usb_curlimit))
710                 dev_err(&pcf->client.dev, "curlim work item already queued\n");
711
712         return 0;
713 }
714 EXPORT_SYMBOL_GPL(pcf50633_notify_usb_current_limit_change);
715
716
717 /* we are run when we see a NOBAT situation, because there is no interrupt
718  * source in pcf50633 that triggers on resuming charging.  It watches to see
719  * if charging resumes, it reassesses the charging source if it does.  If the
720  * USB power disappears, it is also a sign there must be a battery and it is
721  * NOT being charged, so it exits since the next move must be USB insertion for
722  * change of charger state
723  */
724
725 static void pcf50633_work_nobat(struct work_struct *work)
726 {
727         struct pcf50633_data *pcf =
728                         container_of(work, struct pcf50633_data, work_nobat);
729
730         mutex_lock(&pcf->working_lock_nobat);
731         pcf->working_nobat = 1;
732         mutex_unlock(&pcf->working_lock_nobat);
733
734         while (1) {
735                 msleep(1000);
736
737                 if (pcf->suspend_state != PCF50633_SS_RUNNING)
738                         continue;
739
740                 /* there's a battery in there now? */
741                 if (reg_read(pcf, PCF50633_REG_MBCS3) & 0x40) {
742
743                         pcf->jiffies_last_bat_ins = jiffies;
744
745                         /* figure out our charging stance */
746                         add_request_to_adc_queue(pcf, PCF50633_ADCC1_MUX_ADCIN1,
747                                                      PCF50633_ADCC1_AVERAGE_16);
748                         goto bail;
749                 }
750
751                 /* he pulled USB cable since we were started?  exit then */
752                 if (pcf->usb_removal_count_nobat != pcf->usb_removal_count)
753                         goto bail;
754         }
755
756 bail:
757         mutex_lock(&pcf->working_lock_nobat);
758         pcf->working_nobat = 0;
759         mutex_unlock(&pcf->working_lock_nobat);
760 }
761
762
763 static void pcf50633_work(struct work_struct *work)
764 {
765         struct pcf50633_data *pcf =
766                         container_of(work, struct pcf50633_data, work);
767         u_int8_t pcfirq[5];
768         int ret;
769         int tail;
770
771         mutex_lock(&pcf->working_lock);
772         pcf->working = 1;
773
774         /* sanity */
775         if (!&pcf->client.dev)
776                 goto bail;
777
778         /*
779          * if we are presently suspending, we are not in a position to deal
780          * with pcf50633 interrupts at all.
781          *
782          * Because we didn't clear the int pending registers, there will be
783          * no edge / interrupt waiting for us when we wake.  But it is OK
784          * because at the end of our resume, we call this workqueue function
785          * gratuitously, clearing the pending register and re-enabling
786          * servicing this interrupt.
787          */
788
789         if ((pcf->suspend_state == PCF50633_SS_STARTING_SUSPEND) ||
790             (pcf->suspend_state == PCF50633_SS_COMPLETED_SUSPEND))
791                 goto bail;
792
793         /*
794          * If we are inside suspend -> resume completion time we don't attempt
795          * service until we have fully resumed.  Although we could talk to the
796          * device as soon as I2C is up, the regs in the device which we might
797          * choose to modify as part of the service action have not been
798          * reloaded with their pre-suspend states yet.  Therefore we will
799          * defer our service if we are called like that until our resume has
800          * completed.
801          *
802          * This shouldn't happen any more because we disable servicing this
803          * interrupt in suspend and don't re-enable it until resume is
804          * completed.
805          */
806
807         if (pcf->suspend_state &&
808                 (pcf->suspend_state != PCF50633_SS_COMPLETED_RESUME))
809                 goto reschedule;
810
811         /* this is the case early in resume! Sanity check! */
812         if (i2c_get_clientdata(&pcf->client) == NULL)
813                 goto reschedule;
814
815         /*
816         * datasheet says we have to read the five IRQ
817         * status regs in one transaction
818         */
819         ret = i2c_smbus_read_i2c_block_data(&pcf->client,
820                                                 PCF50633_REG_INT1,
821                                                 sizeof(pcfirq),
822                                                 pcfirq);
823         if (ret != sizeof(pcfirq)) {
824                 dev_info(&pcf->client.dev,
825                          "Oh crap PMU IRQ register read failed -- "
826                          "retrying later %d\n", ret);
827                 /*
828                  * it shouldn't fail, we no longer attempt to use
829                  * I2C while it can be suspended.  But we don't have
830                  * much option but to retry if if it ever did fail,
831                  * because if we don't service the interrupt to clear
832                  * it, we will never see another PMU interrupt edge.
833                  */
834                 goto reschedule;
835         }
836
837         /* hey did we just resume? (because we don't get here unless we are
838          * running normally or the first call after resumption)
839          */
840
841         if (pcf->suspend_state != PCF50633_SS_RUNNING) {
842                 /*
843                 * grab a copy of resume interrupt reasons
844                 * from pcf50633 POV
845                 */
846                 memcpy(pcf->pcfirq_resume, pcfirq, sizeof(pcf->pcfirq_resume));
847
848                 /* pcf50633 resume is really really over now then */
849                 pcf->suspend_state = PCF50633_SS_RUNNING;
850         }
851
852         if (!pcf->coldplug_done) {
853                 DEBUGP("PMU Coldplug init\n");
854
855                 /* we used SECOND to kick ourselves started -- turn it off */
856                 pcfirq[0] &= ~PCF50633_INT1_SECOND;
857                 reg_set_bit_mask(pcf, PCF50633_REG_INT1M,
858                                         PCF50633_INT1_SECOND,
859                                         PCF50633_INT1_SECOND);
860
861                 /* coldplug the USB if present */
862                 if ((__reg_read(pcf, PCF50633_REG_MBCS1) &
863                     (PCF50633_MBCS1_USBPRES | PCF50633_MBCS1_USBOK)) ==
864                     (PCF50633_MBCS1_USBPRES | PCF50633_MBCS1_USBOK)) {
865                         DEBUGPC("COLD USBINS\n");
866                         input_report_key(pcf->input_dev, KEY_POWER2, 1);
867                         apm_queue_event(APM_POWER_STATUS_CHANGE);
868                         pcf->flags |= PCF50633_F_USB_PRESENT;
869                         if (pcf->pdata->cb)
870                                 pcf->pdata->cb(&pcf->client.dev,
871                                         PCF50633_FEAT_MBC, PMU_EVT_USB_INSERT);
872                 }
873
874                 /* figure out our initial charging stance */
875                 add_request_to_adc_queue(pcf, PCF50633_ADCC1_MUX_ADCIN1,
876                                               PCF50633_ADCC1_AVERAGE_16);
877
878                 pcf->coldplug_done = 1;
879         }
880
881         DEBUGP("INT1=0x%02x INT2=0x%02x INT3=0x%02x INT4=0x%02x INT5=0x%02x\n",
882                 pcfirq[0], pcfirq[1], pcfirq[2], pcfirq[3], pcfirq[4]);
883
884         if (pcfirq[0] & PCF50633_INT1_ADPINS) {
885                 /* Charger inserted */
886                 DEBUGPC("ADPINS ");
887                 input_report_key(pcf->input_dev, KEY_BATTERY, 1);
888                 apm_queue_event(APM_POWER_STATUS_CHANGE);
889                 pcf->flags |= PCF50633_F_CHG_PRESENT;
890                 if (pcf->pdata->cb)
891                         pcf->pdata->cb(&pcf->client.dev,
892                                        PCF50633_FEAT_MBC, PMU_EVT_INSERT);
893         }
894         if (pcfirq[0] & PCF50633_INT1_ADPREM) {
895                 /* Charger removed */
896                 DEBUGPC("ADPREM ");
897                 input_report_key(pcf->input_dev, KEY_BATTERY, 0);
898                 apm_queue_event(APM_POWER_STATUS_CHANGE);
899                 pcf->flags &= ~PCF50633_F_CHG_PRESENT;
900                 if (pcf->pdata->cb)
901                         pcf->pdata->cb(&pcf->client.dev,
902                                        PCF50633_FEAT_MBC, PMU_EVT_REMOVE);
903         }
904         if (pcfirq[0] & PCF50633_INT1_USBINS) {
905                 DEBUGPC("USBINS ");
906                 input_report_key(pcf->input_dev, KEY_POWER2, 1);
907                 apm_queue_event(APM_POWER_STATUS_CHANGE);
908                 pcf->flags |= PCF50633_F_USB_PRESENT;
909                 if (pcf->pdata->cb)
910                         pcf->pdata->cb(&pcf->client.dev,
911                                        PCF50633_FEAT_MBC, PMU_EVT_USB_INSERT);
912                 msleep(500); /* debounce, allow to see any ID resistor */
913                 /* completion irq will figure out our charging stance */
914                 add_request_to_adc_queue(pcf, PCF50633_ADCC1_MUX_ADCIN1,
915                                      PCF50633_ADCC1_AVERAGE_16);
916         }
917         if (pcfirq[0] & PCF50633_INT1_USBREM) {
918                 DEBUGPC("USBREM ");
919
920                 pcf->usb_removal_count++;
921
922                 /* only deal if we had understood it was in */
923                 if (pcf->flags & PCF50633_F_USB_PRESENT) {
924                         input_report_key(pcf->input_dev, KEY_POWER2, 0);
925                         apm_queue_event(APM_POWER_STATUS_CHANGE);
926                         pcf->flags &= ~PCF50633_F_USB_PRESENT;
927
928                         if (pcf->pdata->cb)
929                                 pcf->pdata->cb(&pcf->client.dev,
930                                         PCF50633_FEAT_MBC, PMU_EVT_USB_REMOVE);
931
932                         /* destroy any memory of grant of power from host */
933                         pcf->last_curlim_set = 0;
934
935                         /* completion irq will figure out our charging stance */
936                         add_request_to_adc_queue(pcf, PCF50633_ADCC1_MUX_ADCIN1,
937                                         PCF50633_ADCC1_AVERAGE_16);
938                 }
939         }
940         if (pcfirq[0] & PCF50633_INT1_ALARM) {
941                 DEBUGPC("ALARM ");
942                 if (pcf->pdata->used_features & PCF50633_FEAT_RTC)
943                         rtc_update_irq(pcf->rtc, 1, RTC_AF | RTC_IRQF);
944         }
945         if (pcfirq[0] & PCF50633_INT1_SECOND) {
946                 DEBUGPC("SECOND ");
947                 if (pcf->flags & PCF50633_F_RTC_SECOND)
948                         rtc_update_irq(pcf->rtc, 1, RTC_PF | RTC_IRQF);
949
950                 if (pcf->onkey_seconds >= 0 &&
951                     pcf->flags & PCF50633_F_PWR_PRESSED) {
952                         DEBUGP("ONKEY_SECONDS(%u, OOCSTAT=0x%02x) ",
953                                 pcf->onkey_seconds,
954                                 reg_read(pcf, PCF50633_REG_OOCSTAT));
955                         pcf->onkey_seconds++;
956                         if (pcf->onkey_seconds >=
957                             pcf->pdata->onkey_seconds_sig_init) {
958                                 /* Ask init to do 'ctrlaltdel' */
959                                 /*
960                                  * currently Linux reacts badly to issuing a
961                                  * signal to PID #1 before init is started.
962                                  * What happens is that the next kernel thread
963                                  * to start, which is the JFFS2 Garbage
964                                  * collector in our case, gets the signal
965                                  * instead and proceeds to fail to fork --
966                                  * which is very bad.  Therefore we confirm
967                                  * PID #1 exists before issuing the signal
968                                  */
969                                 if (find_task_by_pid_ns(1, &init_pid_ns)) {
970                                         kill_pid(task_pid(find_task_by_pid_ns(1, 
971                                                         &init_pid_ns)), SIGPWR, 1);
972                                         DEBUGPC("SIGINT(init) ");
973                                 }
974                                 /* FIXME: what if userspace doesn't shut down? */
975                         }
976                         if (pcf->onkey_seconds >=
977                                 pcf->pdata->onkey_seconds_shutdown) {
978                                 DEBUGPC("Power Off ");
979                                 pcf50633_go_standby();
980                         }
981                 }
982         }
983
984         if (pcfirq[1] & PCF50633_INT2_ONKEYF) {
985                 /* ONKEY falling edge (start of button press) */
986                 DEBUGPC("ONKEYF ");
987                 pcf->flags |= PCF50633_F_PWR_PRESSED;
988                 input_report_key(pcf->input_dev, KEY_POWER, 1);
989         }
990         if (pcfirq[1] & PCF50633_INT2_ONKEYR) {
991                 /* ONKEY rising edge (end of button press) */
992                 DEBUGPC("ONKEYR ");
993                 pcf->flags &= ~PCF50633_F_PWR_PRESSED;
994                 pcf->onkey_seconds = -1;
995                 input_report_key(pcf->input_dev, KEY_POWER, 0);
996                 /* disable SECOND interrupt in case RTC didn't
997                  * request it */
998                 if (!(pcf->flags & PCF50633_F_RTC_SECOND))
999                         reg_set_bit_mask(pcf, PCF50633_REG_INT1M,
1000                                          PCF50633_INT1_SECOND,
1001                                          PCF50633_INT1_SECOND);
1002         }
1003         /* FIXME: we don't use EXTON1/2/3. thats why we skip it */
1004
1005         if (pcfirq[2] & PCF50633_INT3_BATFULL) {
1006                 DEBUGPC("BATFULL ");
1007
1008                 /* the problem is, we get a false BATFULL if we inserted battery
1009                  * while USB powered.  Defeat BATFULL if we recently inserted
1010                  * battery
1011                  */
1012
1013                 if ((jiffies - pcf->jiffies_last_bat_ins) < (HZ * 2)) {
1014
1015                         DEBUGPC("*** Ignoring BATFULL ***\n");
1016
1017                         ret = reg_read(pcf, PCF50633_REG_MBCC7) &
1018                                         PCF56033_MBCC7_USB_MASK;
1019
1020
1021                         reg_set_bit_mask(pcf, PCF50633_REG_MBCC7,
1022                                          PCF56033_MBCC7_USB_MASK,
1023                                          PCF50633_MBCC7_USB_SUSPEND);
1024
1025                         reg_set_bit_mask(pcf, PCF50633_REG_MBCC7,
1026                                          PCF56033_MBCC7_USB_MASK,
1027                                          ret);
1028                 } else {
1029                         if (pcf->pdata->cb)
1030                                 pcf->pdata->cb(&pcf->client.dev,
1031                                        PCF50633_FEAT_MBC, PMU_EVT_CHARGER_IDLE);
1032                 }
1033
1034                 /* FIXME: signal this to userspace */
1035         }
1036         if (pcfirq[2] & PCF50633_INT3_CHGHALT) {
1037                 DEBUGPC("CHGHALT ");
1038                 /*
1039                  * this is really "battery not pulling current" -- it can
1040                  * appear with no battery attached
1041                  */
1042                 if (pcf->pdata->cb)
1043                         pcf->pdata->cb(&pcf->client.dev,
1044                                        PCF50633_FEAT_MBC, PMU_EVT_CHARGER_CHANGE);
1045         }
1046         if (pcfirq[2] & PCF50633_INT3_THLIMON) {
1047                 DEBUGPC("THLIMON ");
1048                 pcf->flags |= PCF50633_F_CHG_PROT;
1049                 if (pcf->pdata->cb)
1050                         pcf->pdata->cb(&pcf->client.dev,
1051                                        PCF50633_FEAT_MBC, PMU_EVT_CHARGER_CHANGE);
1052         }
1053         if (pcfirq[2] & PCF50633_INT3_THLIMOFF) {
1054                 DEBUGPC("THLIMOFF ");
1055                 pcf->flags &= ~PCF50633_F_CHG_PROT;
1056                 if (pcf->pdata->cb)
1057                         pcf->pdata->cb(&pcf->client.dev,
1058                                        PCF50633_FEAT_MBC, PMU_EVT_CHARGER_CHANGE);
1059         }
1060         if (pcfirq[2] & PCF50633_INT3_USBLIMON) {
1061                 DEBUGPC("USBLIMON ");
1062                 if (pcf->pdata->cb)
1063                         pcf->pdata->cb(&pcf->client.dev,
1064                                        PCF50633_FEAT_MBC, PMU_EVT_CHARGER_CHANGE);
1065         }
1066         if (pcfirq[2] & PCF50633_INT3_USBLIMOFF) {
1067                 DEBUGPC("USBLIMOFF ");
1068                 if (pcf->pdata->cb)
1069                         pcf->pdata->cb(&pcf->client.dev,
1070                                        PCF50633_FEAT_MBC, PMU_EVT_CHARGER_CHANGE);
1071         }
1072         if (pcfirq[2] & PCF50633_INT3_ADCRDY) {
1073                 /* ADC result ready */
1074                 DEBUGPC("ADCRDY ");
1075                 tail = pcf->adc_queue_tail;
1076                 pcf->adc_queue_tail = (pcf->adc_queue_tail + 1) &
1077                                       (MAX_ADC_FIFO_DEPTH - 1);
1078
1079                 switch (pcf->adc_queue_mux[tail]) {
1080                 case PCF50633_ADCC1_MUX_BATSNS_RES: /* battery voltage */
1081                         pcf->flag_bat_voltage_read = async_adc_complete(pcf);
1082                         break;
1083                 case PCF50633_ADCC1_MUX_ADCIN1: /* charger type */
1084                         pcf->charger_adc_result_raw = async_adc_complete(pcf);
1085                         pcf->charger_type = interpret_charger_type_from_adc(
1086                                              pcf, pcf->charger_adc_result_raw);
1087                         configure_pmu_for_charger(pcf, pcf->charger_type);
1088                         break;
1089                 default:
1090                         async_adc_complete(pcf);
1091                         break;
1092                 }
1093                 trigger_next_adc_job_if_any(pcf);
1094         }
1095         if (pcfirq[2] & PCF50633_INT3_ONKEY1S) {
1096                 /* ONKEY pressed for more than 1 second */
1097                 pcf->onkey_seconds = 0;
1098                 DEBUGPC("ONKEY1S ");
1099                 /* Tell PMU we are taking care of this */
1100                 reg_set_bit_mask(pcf, PCF50633_REG_OOCSHDWN,
1101                                  PCF50633_OOCSHDWN_TOTRST,
1102                                  PCF50633_OOCSHDWN_TOTRST);
1103                 /* enable SECOND interrupt (hz tick) */
1104                 reg_clear_bits(pcf, PCF50633_REG_INT1M, PCF50633_INT1_SECOND);
1105         }
1106
1107         if (pcfirq[3] & (PCF50633_INT4_LOWBAT|PCF50633_INT4_LOWSYS)) {
1108                 if ((__reg_read(pcf, PCF50633_REG_MBCS1) &
1109                     (PCF50633_MBCS1_USBPRES | PCF50633_MBCS1_USBOK)) ==
1110                     (PCF50633_MBCS1_USBPRES | PCF50633_MBCS1_USBOK)) {
1111                         /*
1112                          * hey no need to freak out, we have some kind of
1113                          * valid charger power to keep us going -- but note that
1114                          * we are not actually charging anything
1115                          */
1116                         if (pcf->pdata->cb)
1117                                 pcf->pdata->cb(&pcf->client.dev,
1118                                        PCF50633_FEAT_MBC, PMU_EVT_CHARGER_IDLE);
1119
1120                         reg_set_bit_mask(pcf, PCF50633_REG_MBCC1,
1121                                         PCF50633_MBCC1_RESUME,
1122                                         PCF50633_MBCC1_RESUME);
1123         
1124                         /*
1125                          * Well, we are not charging anything right this second
1126                          * ... however in the next ~30s before we get the next
1127                          * NOBAT, he might insert a battery.  So we schedule a
1128                          * work function checking to see if
1129                          * we started charging something during that time.
1130                          * USB removal as well as charging terminates the work
1131                          * function so we can't get terminally confused
1132                          */
1133                         mutex_lock(&pcf->working_lock_nobat);
1134                         if (!pcf->working_nobat) {
1135                                 pcf->usb_removal_count_nobat =
1136                                                         pcf->usb_removal_count;
1137
1138                                 if (!schedule_work(&pcf->work_nobat))
1139                                         DEBUGPC("failed to schedule nobat\n");
1140                         }
1141                         mutex_unlock(&pcf->working_lock_nobat);
1142
1143
1144                         DEBUGPC("(NO)BAT ");
1145                 } else {
1146                         /* Really low battery voltage, we have 8 seconds left */
1147                         DEBUGPC("LOWBAT ");
1148                         /*
1149                          * currently Linux reacts badly to issuing a signal to
1150                          * PID #1 before init is started.  What happens is that
1151                          * the next kernel thread to start, which is the JFFS2
1152                          * Garbage collector in our case, gets the signal
1153                          * instead and proceeds to fail to fork -- which is
1154                          * very bad.  Therefore we confirm PID #1 exists
1155                          * before issuing SPIGPWR
1156                          */
1157
1158                         if (find_task_by_pid_ns(1, &init_pid_ns)) {
1159                                 apm_queue_event(APM_LOW_BATTERY);
1160                                 DEBUGPC("SIGPWR(init) ");
1161                                 kill_pid(task_pid(find_task_by_pid_ns(1, &init_pid_ns)), SIGPWR, 1);
1162                         } else
1163                                 /*
1164                                  * well, our situation is like this:  we do not
1165                                  * have any external power, we have a low
1166                                  * battery and since PID #1 doesn't exist yet,
1167                                  * we are early in the boot, likely before
1168                                  * rootfs mount.  We should just call it a day
1169                                  */
1170                                 apm_queue_event(APM_CRITICAL_SUSPEND);
1171                 }
1172
1173                 /* Tell PMU we are taking care of this */
1174                 reg_set_bit_mask(pcf, PCF50633_REG_OOCSHDWN,
1175                                  PCF50633_OOCSHDWN_TOTRST,
1176                                  PCF50633_OOCSHDWN_TOTRST);
1177         }
1178         if (pcfirq[3] & PCF50633_INT4_HIGHTMP) {
1179                 /* High temperature */
1180                 DEBUGPC("HIGHTMP ");
1181                 apm_queue_event(APM_CRITICAL_SUSPEND);
1182         }
1183         if (pcfirq[3] & PCF50633_INT4_AUTOPWRFAIL) {
1184                 DEBUGPC("PCF50633_INT4_AUTOPWRFAIL ");
1185                 /* FIXME: deal with this */
1186         }
1187         if (pcfirq[3] & PCF50633_INT4_DWN1PWRFAIL) {
1188                 DEBUGPC("PCF50633_INT4_DWN1PWRFAIL ");
1189                 /* FIXME: deal with this */
1190         }
1191         if (pcfirq[3] & PCF50633_INT4_DWN2PWRFAIL) {
1192                 DEBUGPC("PCF50633_INT4_DWN2PWRFAIL ");
1193                 /* FIXME: deal with this */
1194         }
1195         if (pcfirq[3] & PCF50633_INT4_LEDPWRFAIL) {
1196                 DEBUGPC("PCF50633_INT4_LEDPWRFAIL ");
1197                 /* FIXME: deal with this */
1198         }
1199         if (pcfirq[3] & PCF50633_INT4_LEDOVP) {
1200                 DEBUGPC("PCF50633_INT4_LEDOVP ");
1201                 /* FIXME: deal with this */
1202         }
1203
1204         DEBUGPC("\n");
1205
1206 bail:
1207         pcf->working = 0;
1208         input_sync(pcf->input_dev);
1209         put_device(&pcf->client.dev);
1210         mutex_unlock(&pcf->working_lock);
1211
1212         return;
1213
1214 reschedule:
1215         /* don't spew, delaying whatever else is happening */
1216         /* EXCEPTION: if we are in the middle of suspending, we don't have
1217          * time to hang around since we may be turned off core 1V3 already
1218          */
1219         if ((pcf->suspend_state != PCF50633_SS_STARTING_SUSPEND) &&
1220             (pcf->suspend_state != PCF50633_SS_COMPLETED_SUSPEND)) {
1221                 msleep(10);
1222                 dev_dbg(&pcf->client.dev, "rescheduling interrupt service\n");
1223         }
1224         if (!schedule_work(&pcf->work))
1225                 dev_err(&pcf->client.dev, "int service reschedule failed\n");
1226
1227         /* we don't put the device here, hold it for next time */
1228         mutex_unlock(&pcf->working_lock);
1229 }
1230
1231 static irqreturn_t pcf50633_irq(int irq, void *_pcf)
1232 {
1233         struct pcf50633_data *pcf = _pcf;
1234
1235         DEBUGP("entering(irq=%u, pcf=%p): scheduling work\n", irq, _pcf);
1236         dev_dbg(&pcf->client.dev, "pcf50633_irq scheduling work\n");
1237
1238         get_device(&pcf->client.dev);
1239         if (!schedule_work(&pcf->work) && !pcf->working)
1240                 dev_err(&pcf->client.dev, "pcf irq work already queued\n");
1241
1242         return IRQ_HANDLED;
1243 }
1244
1245 static u_int16_t adc_to_batt_millivolts(u_int16_t adc)
1246 {
1247         u_int16_t mvolts;
1248
1249         mvolts = (adc * 6000) / 1024;
1250
1251         return mvolts;
1252 }
1253
1254 #define BATTVOLT_SCALE_START 2800
1255 #define BATTVOLT_SCALE_END 4200
1256 #define BATTVOLT_SCALE_DIVIDER ((BATTVOLT_SCALE_END - BATTVOLT_SCALE_START)/100)
1257
1258 static u_int8_t battvolt_scale(u_int16_t battvolt)
1259 {
1260         /* FIXME: this linear scale is completely bogus */
1261         u_int16_t battvolt_relative = battvolt - BATTVOLT_SCALE_START;
1262         unsigned int percent = battvolt_relative / BATTVOLT_SCALE_DIVIDER;
1263
1264         return percent;
1265 }
1266
1267 u_int16_t pcf50633_battvolt(struct pcf50633_data *pcf)
1268 {
1269         int count = 10;
1270
1271         pcf->flag_bat_voltage_read = -1;
1272         add_request_to_adc_queue(pcf, PCF50633_ADCC1_MUX_BATSNS_RES,
1273                                       PCF50633_ADCC1_AVERAGE_16);
1274
1275         while ((count--) && (pcf->flag_bat_voltage_read < 0))
1276                 msleep(1);
1277
1278         if (count < 0) { /* timeout somehow */
1279                 DEBUGPC("pcf50633_battvolt timeout :-(\n");
1280                 return -1;
1281         }
1282
1283         return adc_to_batt_millivolts(pcf->flag_bat_voltage_read);
1284 }
1285 EXPORT_SYMBOL_GPL(pcf50633_battvolt);
1286
1287 static ssize_t show_battvolt(struct device *dev, struct device_attribute *attr,
1288                              char *buf)
1289 {
1290         struct i2c_client *client = to_i2c_client(dev);
1291         struct pcf50633_data *pcf = i2c_get_clientdata(client);
1292
1293         return sprintf(buf, "%u\n", pcf50633_battvolt(pcf));
1294 }
1295 static DEVICE_ATTR(battvolt, S_IRUGO | S_IWUSR, show_battvolt, NULL);
1296
1297 static int reg_id_by_name(const char *name)
1298 {
1299         int reg_id;
1300
1301         if (!strcmp(name, "voltage_auto"))
1302                 reg_id = PCF50633_REGULATOR_AUTO;
1303         else if (!strcmp(name, "voltage_down1"))
1304                 reg_id = PCF50633_REGULATOR_DOWN1;
1305         else if (!strcmp(name, "voltage_down2"))
1306                 reg_id = PCF50633_REGULATOR_DOWN2;
1307         else if (!strcmp(name, "voltage_memldo"))
1308                 reg_id = PCF50633_REGULATOR_MEMLDO;
1309         else if (!strcmp(name, "voltage_ldo1"))
1310                 reg_id = PCF50633_REGULATOR_LDO1;
1311         else if (!strcmp(name, "voltage_ldo2"))
1312                 reg_id = PCF50633_REGULATOR_LDO2;
1313         else if (!strcmp(name, "voltage_ldo3"))
1314                 reg_id = PCF50633_REGULATOR_LDO3;
1315         else if (!strcmp(name, "voltage_ldo4"))
1316                 reg_id = PCF50633_REGULATOR_LDO4;
1317         else if (!strcmp(name, "voltage_ldo5"))
1318                 reg_id = PCF50633_REGULATOR_LDO5;
1319         else if (!strcmp(name, "voltage_ldo6"))
1320                 reg_id = PCF50633_REGULATOR_LDO6;
1321         else if (!strcmp(name, "voltage_hcldo"))
1322                 reg_id = PCF50633_REGULATOR_HCLDO;
1323         else
1324                 reg_id = -1;
1325
1326         return reg_id;
1327 }
1328
1329 static ssize_t show_vreg(struct device *dev, struct device_attribute *attr,
1330                          char *buf)
1331 {
1332         struct i2c_client *client = to_i2c_client(dev);
1333         struct pcf50633_data *pcf = i2c_get_clientdata(client);
1334         unsigned int reg_id;
1335
1336         reg_id = reg_id_by_name(attr->attr.name);
1337         if (reg_id < 0)
1338                 return 0;
1339
1340         if (pcf50633_onoff_get(pcf, reg_id) > 0)
1341                 return sprintf(buf, "%u\n", pcf50633_voltage_get(pcf, reg_id));
1342         else
1343                 return strlcpy(buf, "0\n", PAGE_SIZE);
1344 }
1345
1346 static ssize_t set_vreg(struct device *dev, struct device_attribute *attr,
1347                         const char *buf, size_t count)
1348 {
1349         struct i2c_client *client = to_i2c_client(dev);
1350         struct pcf50633_data *pcf = i2c_get_clientdata(client);
1351         unsigned long mvolts = simple_strtoul(buf, NULL, 10);
1352         unsigned int reg_id;
1353
1354         reg_id = reg_id_by_name(attr->attr.name);
1355         if (reg_id < 0)
1356                 return -EIO;
1357
1358         DEBUGP("attempting to set %s(%d) to %lu mvolts\n", attr->attr.name,
1359                 reg_id, mvolts);
1360
1361         if (mvolts == 0) {
1362                 pcf50633_onoff_set(pcf, reg_id, 0);
1363         } else {
1364                 if (pcf50633_voltage_set(pcf, reg_id, mvolts) < 0) {
1365                         dev_warn(dev, "refusing to set %s(%d) to %lu mvolts "
1366                                  "(max=%u)\n", attr->attr.name, reg_id, mvolts,
1367                                  pcf->pdata->rails[reg_id].voltage.max);
1368                         return -EINVAL;
1369                 }
1370                 pcf50633_onoff_set(pcf, reg_id, 1);
1371         }
1372
1373         return count;
1374 }
1375
1376 static DEVICE_ATTR(voltage_auto, S_IRUGO | S_IWUSR, show_vreg, set_vreg);
1377 static DEVICE_ATTR(voltage_down1, S_IRUGO | S_IWUSR, show_vreg, set_vreg);
1378 static DEVICE_ATTR(voltage_down2, S_IRUGO | S_IWUSR, show_vreg, set_vreg);
1379 static DEVICE_ATTR(voltage_memldo, S_IRUGO | S_IWUSR, show_vreg, set_vreg);
1380 static DEVICE_ATTR(voltage_ldo1, S_IRUGO | S_IWUSR, show_vreg, set_vreg);
1381 static DEVICE_ATTR(voltage_ldo2, S_IRUGO | S_IWUSR, show_vreg, set_vreg);
1382 static DEVICE_ATTR(voltage_ldo3, S_IRUGO | S_IWUSR, show_vreg, set_vreg);
1383 static DEVICE_ATTR(voltage_ldo4, S_IRUGO | S_IWUSR, show_vreg, set_vreg);
1384 static DEVICE_ATTR(voltage_ldo5, S_IRUGO | S_IWUSR, show_vreg, set_vreg);
1385 static DEVICE_ATTR(voltage_ldo6, S_IRUGO | S_IWUSR, show_vreg, set_vreg);
1386 static DEVICE_ATTR(voltage_hcldo, S_IRUGO | S_IWUSR, show_vreg, set_vreg);
1387
1388 /***********************************************************************
1389  * Charger Control
1390  ***********************************************************************/
1391
1392 /* Set maximum USB current limit */
1393 static void pcf50633_usb_curlim_set(struct pcf50633_data *pcf, int ma)
1394 {
1395         u_int8_t bits;
1396         int active = 0;
1397
1398         pcf->last_curlim_set = ma;
1399
1400         dev_dbg(&pcf->client.dev, "setting usb current limit to %d ma", ma);
1401
1402         if (ma >= 1000) {
1403                 bits = PCF50633_MBCC7_USB_1000mA;
1404         }
1405         else if (ma >= 500)
1406                 bits = PCF50633_MBCC7_USB_500mA;
1407         else if (ma >= 100)
1408                 bits = PCF50633_MBCC7_USB_100mA;
1409         else
1410                 bits = PCF50633_MBCC7_USB_SUSPEND;
1411
1412         /* set the nearest charging limit */
1413         reg_set_bit_mask(pcf, PCF50633_REG_MBCC7, PCF56033_MBCC7_USB_MASK,
1414                          bits);
1415
1416         /* with this charging limit, is charging actually meaningful? */
1417         switch (bits) {
1418         case PCF50633_MBCC7_USB_500mA:
1419         case PCF50633_MBCC7_USB_1000mA:
1420                 /* yes with this charging limit, we can do real charging */
1421                 active = 1;
1422                 break;
1423         default: /* right charging context that if there is power, we charge */
1424                 if (pcf->flags & PCF50633_F_USB_PRESENT)
1425                         pcf->pdata->cb(&pcf->client.dev,
1426                                PCF50633_FEAT_MBC, PMU_EVT_CHARGER_ACTIVE);
1427                 break;
1428         }
1429         /*
1430          * enable or disable charging according to current limit -- this will
1431          * also throw a platform notification callback about it
1432          */
1433         pcf50633_charge_enable(pcf50633_global, active);
1434
1435         /* clear batfull */
1436         reg_set_bit_mask(pcf, PCF50633_REG_MBCC1,
1437                                 PCF50633_MBCC1_AUTORES,
1438                                 0);
1439         reg_set_bit_mask(pcf, PCF50633_REG_MBCC1,
1440                                 PCF50633_MBCC1_RESUME,
1441                                 PCF50633_MBCC1_RESUME);
1442         reg_set_bit_mask(pcf, PCF50633_REG_MBCC1,
1443                                 PCF50633_MBCC1_AUTORES,
1444                                 PCF50633_MBCC1_AUTORES);
1445
1446 }
1447
1448 static ssize_t show_usblim(struct device *dev, struct device_attribute *attr,
1449                            char *buf)
1450 {
1451         struct i2c_client *client = to_i2c_client(dev);
1452         struct pcf50633_data *pcf = i2c_get_clientdata(client);
1453         u_int8_t usblim = reg_read(pcf, PCF50633_REG_MBCC7) &
1454                                                 PCF56033_MBCC7_USB_MASK;
1455         unsigned int ma;
1456
1457         if (usblim == PCF50633_MBCC7_USB_1000mA)
1458                 ma = 1000;
1459         else if (usblim == PCF50633_MBCC7_USB_500mA)
1460                 ma = 500;
1461         else if (usblim == PCF50633_MBCC7_USB_100mA)
1462                 ma = 100;
1463         else
1464                 ma = 0;
1465
1466         return sprintf(buf, "%u\n", ma);
1467 }
1468 static DEVICE_ATTR(usb_curlim, S_IRUGO | S_IWUSR, show_usblim, NULL);
1469
1470 /* Enable/disable charging */
1471 static void pcf50633_charge_enable(struct pcf50633_data *pcf, int on)
1472 {
1473         u_int8_t bits;
1474         u_int8_t usblim;
1475
1476         if (!(pcf->pdata->used_features & PCF50633_FEAT_MBC))
1477                 return;
1478
1479         DEBUGPC("pcf50633_charge_enable %d\n", on);
1480
1481         if (on) {
1482                 pcf->flags |= PCF50633_F_CHG_ENABLED;
1483                 bits = PCF50633_MBCC1_CHGENA;
1484                 usblim = reg_read(pcf, PCF50633_REG_MBCC7) &
1485                                                         PCF56033_MBCC7_USB_MASK;
1486                 switch (usblim) {
1487                 case PCF50633_MBCC7_USB_1000mA:
1488                 case PCF50633_MBCC7_USB_500mA:
1489                         if (pcf->flags & PCF50633_F_USB_PRESENT)
1490                                 if (pcf->pdata->cb)
1491                                         pcf->pdata->cb(&pcf->client.dev,
1492                                                        PCF50633_FEAT_MBC,
1493                                                        PMU_EVT_CHARGER_ACTIVE);
1494                         break;
1495                 default:
1496                         break;
1497                 }
1498         } else {
1499                 pcf->flags &= ~PCF50633_F_CHG_ENABLED;
1500                 bits = 0;
1501                 if (pcf->pdata->cb)
1502                         pcf->pdata->cb(&pcf->client.dev,
1503                                        PCF50633_FEAT_MBC, PMU_EVT_CHARGER_IDLE);
1504         }
1505         reg_set_bit_mask(pcf, PCF50633_REG_MBCC1, PCF50633_MBCC1_CHGENA,
1506                          bits);
1507 }
1508
1509 #if 0
1510 #define ONE                     1000000
1511 static u_int16_t adc_to_rntc(struct pcf50633_data *pcf, u_int16_t adc)
1512 {
1513         u_int32_t r_batt = (adc * pcf->pdata->r_fix_batt) / (1023 - adc);
1514         u_int16_t r_ntc;
1515
1516         /* The battery NTC has a parallell 10kOhms resistor */
1517         r_ntc = ONE / ((ONE/r_batt) - (ONE/pcf->pdata->r_fix_batt_par));
1518
1519         return r_ntc;
1520 }
1521 #endif
1522 static ssize_t show_battemp(struct device *dev, struct device_attribute *attr,
1523                             char *buf)
1524 {
1525         return sprintf(buf, "\n");
1526 }
1527 static DEVICE_ATTR(battemp, S_IRUGO | S_IWUSR, show_battemp, NULL);
1528 #if 0
1529 static u_int16_t adc_to_chg_milliamps(struct pcf50633_data *pcf,
1530                                              u_int16_t adc_adcin1,
1531                                              u_int16_t adc_batvolt)
1532 {
1533         u_int32_t res = ((adc_adcin1 - adc_batvolt) * 6000);
1534         return res / (pcf->pdata->r_sense_milli * 1024 / 1000);
1535 }
1536 #endif
1537 static ssize_t show_chgcur(struct device *dev, struct device_attribute *attr,
1538                            char *buf)
1539 {
1540         return sprintf(buf, "\n");
1541 }
1542 static DEVICE_ATTR(chgcur, S_IRUGO | S_IWUSR, show_chgcur, NULL);
1543
1544 static const char *chgmode_names[] = {
1545         [PCF50633_MBCS2_MBC_PLAY]               = "play-only",
1546         [PCF50633_MBCS2_MBC_USB_PRE]            = "pre",
1547         [PCF50633_MBCS2_MBC_ADP_PRE]            = "pre",
1548         [PCF50633_MBCS2_MBC_USB_PRE_WAIT]       = "pre-wait",
1549         [PCF50633_MBCS2_MBC_ADP_PRE_WAIT]       = "pre-wait",
1550         [PCF50633_MBCS2_MBC_USB_FAST]           = "fast",
1551         [PCF50633_MBCS2_MBC_ADP_FAST]           = "fast",
1552         [PCF50633_MBCS2_MBC_USB_FAST_WAIT]      = "fast-wait",
1553         [PCF50633_MBCS2_MBC_ADP_FAST_WAIT]      = "fast-wait",
1554         [PCF50633_MBCS2_MBC_ADP_FAST_WAIT]      = "bat-full",
1555 };
1556
1557 static ssize_t show_chgmode(struct device *dev, struct device_attribute *attr,
1558                             char *buf)
1559 {
1560         struct i2c_client *client = to_i2c_client(dev);
1561         struct pcf50633_data *pcf = i2c_get_clientdata(client);
1562         u_int8_t mbcs2 = reg_read(pcf, PCF50633_REG_MBCS2);
1563         u_int8_t chgmod = (mbcs2 & PCF50633_MBCS2_MBC_MASK);
1564
1565         return sprintf(buf, "%s\n", chgmode_names[chgmod]);
1566 }
1567
1568 static ssize_t set_chgmode(struct device *dev, struct device_attribute *attr,
1569                            const char *buf, size_t count)
1570 {
1571         struct i2c_client *client = to_i2c_client(dev);
1572         struct pcf50633_data *pcf = i2c_get_clientdata(client);
1573
1574         /* As opposed to the PCF50606, we can only enable or disable
1575          * charging and not directly jump into a certain mode! */
1576
1577         if (!strcmp(buf, "0\n"))
1578                 pcf50633_charge_enable(pcf, 0);
1579         else
1580                 pcf50633_charge_enable(pcf, 1);
1581
1582         return count;
1583 }
1584
1585 static DEVICE_ATTR(chgmode, S_IRUGO | S_IWUSR, show_chgmode, set_chgmode);
1586
1587 static const char *chgstate_names[] = {
1588         [PCF50633_FIDX_CHG_ENABLED]             = "enabled",
1589         [PCF50633_FIDX_CHG_PRESENT]             = "charger_present",
1590         [PCF50633_FIDX_USB_PRESENT]             = "usb_present",
1591         [PCF50633_FIDX_CHG_ERR]                 = "error",
1592         [PCF50633_FIDX_CHG_PROT]                = "protection",
1593         [PCF50633_FIDX_CHG_READY]               = "ready",
1594 };
1595
1596 static ssize_t show_chgstate(struct device *dev, struct device_attribute *attr,
1597                             char *buf)
1598 {
1599         struct i2c_client *client = to_i2c_client(dev);
1600         struct pcf50633_data *pcf = i2c_get_clientdata(client);
1601
1602         char *b = buf;
1603         int i;
1604
1605         for (i = 0; i < 32; i++)
1606                 if (pcf->flags & (1 << i) && i < ARRAY_SIZE(chgstate_names))
1607                         b += sprintf(b, "%s ", chgstate_names[i]);
1608
1609         if (b > buf)
1610                 b += sprintf(b, "\n");
1611
1612         return b - buf;
1613 }
1614 static DEVICE_ATTR(chgstate, S_IRUGO | S_IWUSR, show_chgstate, NULL);
1615
1616 /***********************************************************************
1617  * APM emulation
1618  ***********************************************************************/
1619
1620 extern void (*apm_get_power_status)(struct apm_power_info *);
1621
1622 static void pcf50633_get_power_status(struct apm_power_info *info)
1623 {
1624         struct pcf50633_data *pcf = pcf50633_global;
1625         u_int8_t chgmod = reg_read(pcf, PCF50633_REG_MBCS2) &
1626                                    PCF50633_MBCS2_MBC_MASK;
1627
1628         u_int16_t battvolt = pcf50633_battvolt(pcf);
1629
1630         if (reg_read(pcf, PCF50633_REG_MBCS1) &
1631                         (PCF50633_MBCS1_USBPRES|PCF50633_MBCS1_ADAPTPRES))
1632                 info->ac_line_status = APM_AC_ONLINE;
1633         else
1634                 info->ac_line_status = APM_AC_OFFLINE;
1635
1636         switch (chgmod) {
1637         case PCF50633_MBCS2_MBC_PLAY:
1638         case PCF50633_MBCS2_MBC_USB_PRE:
1639         case PCF50633_MBCS2_MBC_USB_PRE_WAIT:
1640         case PCF50633_MBCS2_MBC_USB_FAST_WAIT:
1641         case PCF50633_MBCS2_MBC_ADP_PRE:
1642         case PCF50633_MBCS2_MBC_ADP_PRE_WAIT:
1643         case PCF50633_MBCS2_MBC_ADP_FAST_WAIT:
1644         case PCF50633_MBCS2_MBC_BAT_FULL:
1645         case PCF50633_MBCS2_MBC_HALT:
1646                 info->battery_life = battvolt_scale(battvolt);
1647                 break;
1648         case PCF50633_MBCS2_MBC_USB_FAST:
1649         case PCF50633_MBCS2_MBC_ADP_FAST:
1650                 info->battery_status = APM_BATTERY_STATUS_CHARGING;
1651                 info->battery_flag = APM_BATTERY_FLAG_CHARGING;
1652         default:
1653                 break;
1654         }
1655 }
1656
1657 /***********************************************************************
1658  * RTC
1659  ***********************************************************************/
1660 enum pcf50633_time_indexes {
1661         PCF50633_TI_SEC = 0,
1662         PCF50633_TI_MIN,
1663         PCF50633_TI_HOUR,
1664         PCF50633_TI_WKDAY,
1665         PCF50633_TI_DAY,
1666         PCF50633_TI_MONTH,
1667         PCF50633_TI_YEAR,
1668         PCF50633_TI_EXTENT /* always last */
1669 };
1670
1671
1672 struct pcf50633_time {
1673         u_int8_t time[PCF50633_TI_EXTENT];
1674 };
1675
1676 static void pcf2rtc_time(struct rtc_time *rtc, struct pcf50633_time *pcf)
1677 {
1678         rtc->tm_sec = BCD2BIN(pcf->time[PCF50633_TI_SEC]);
1679         rtc->tm_min = BCD2BIN(pcf->time[PCF50633_TI_MIN]);
1680         rtc->tm_hour = BCD2BIN(pcf->time[PCF50633_TI_HOUR]);
1681         rtc->tm_wday = BCD2BIN(pcf->time[PCF50633_TI_WKDAY]);
1682         rtc->tm_mday = BCD2BIN(pcf->time[PCF50633_TI_DAY]);
1683         rtc->tm_mon = BCD2BIN(pcf->time[PCF50633_TI_MONTH]);
1684         rtc->tm_year = BCD2BIN(pcf->time[PCF50633_TI_YEAR]) + 100;
1685 }
1686
1687 static void rtc2pcf_time(struct pcf50633_time *pcf, struct rtc_time *rtc)
1688 {
1689         pcf->time[PCF50633_TI_SEC] = BIN2BCD(rtc->tm_sec);
1690         pcf->time[PCF50633_TI_MIN] = BIN2BCD(rtc->tm_min);
1691         pcf->time[PCF50633_TI_HOUR] = BIN2BCD(rtc->tm_hour);
1692         pcf->time[PCF50633_TI_WKDAY] = BIN2BCD(rtc->tm_wday);
1693         pcf->time[PCF50633_TI_DAY] = BIN2BCD(rtc->tm_mday);
1694         pcf->time[PCF50633_TI_MONTH] = BIN2BCD(rtc->tm_mon);
1695         pcf->time[PCF50633_TI_YEAR] = BIN2BCD(rtc->tm_year - 100);
1696 }
1697
1698 static int pcf50633_rtc_ioctl(struct device *dev, unsigned int cmd,
1699                               unsigned long arg)
1700 {
1701         struct i2c_client *client = to_i2c_client(dev);
1702         struct pcf50633_data *pcf = i2c_get_clientdata(client);
1703
1704         switch (cmd) {
1705         case RTC_AIE_OFF:
1706                 /* disable the alarm interrupt */
1707                 reg_set_bit_mask(pcf, PCF50633_REG_INT1M,
1708                                  PCF50633_INT1_ALARM, PCF50633_INT1_ALARM);
1709                 return 0;
1710         case RTC_AIE_ON:
1711                 /* enable the alarm interrupt */
1712                 reg_clear_bits(pcf, PCF50633_REG_INT1M, PCF50633_INT1_ALARM);
1713                 return 0;
1714         case RTC_PIE_OFF:
1715                 /* disable periodic interrupt (hz tick) */
1716                 pcf->flags &= ~PCF50633_F_RTC_SECOND;
1717                 reg_set_bit_mask(pcf, PCF50633_REG_INT1M,
1718                                  PCF50633_INT1_SECOND, PCF50633_INT1_SECOND);
1719                 return 0;
1720         case RTC_PIE_ON:
1721                 /* ensable periodic interrupt (hz tick) */
1722                 pcf->flags |= PCF50633_F_RTC_SECOND;
1723                 reg_clear_bits(pcf, PCF50633_REG_INT1M, PCF50633_INT1_SECOND);
1724                 return 0;
1725         }
1726         return -ENOIOCTLCMD;
1727 }
1728
1729 static int pcf50633_rtc_read_time(struct device *dev, struct rtc_time *tm)
1730 {
1731         struct i2c_client *client = to_i2c_client(dev);
1732         struct pcf50633_data *pcf = i2c_get_clientdata(client);
1733         struct pcf50633_time pcf_tm;
1734         int ret;
1735
1736         mutex_lock(&pcf->lock);
1737
1738         ret = i2c_smbus_read_i2c_block_data(&pcf->client,
1739                                             PCF50633_REG_RTCSC,
1740                                             PCF50633_TI_EXTENT,
1741                                             &pcf_tm.time[0]);
1742         if (ret != PCF50633_TI_EXTENT)
1743                 dev_err(dev, "Failed to read time :-(\n");
1744
1745         mutex_unlock(&pcf->lock);
1746
1747         dev_dbg(dev, "PCF_TIME: %02x.%02x.%02x %02x:%02x:%02x\n",
1748                 pcf_tm.time[PCF50633_TI_DAY],
1749                 pcf_tm.time[PCF50633_TI_MONTH],
1750                 pcf_tm.time[PCF50633_TI_YEAR],
1751                 pcf_tm.time[PCF50633_TI_HOUR],
1752                 pcf_tm.time[PCF50633_TI_MIN],
1753                 pcf_tm.time[PCF50633_TI_SEC]);
1754
1755         pcf2rtc_time(tm, &pcf_tm);
1756
1757         dev_dbg(dev, "RTC_TIME: %u.%u.%u %u:%u:%u\n",
1758                 tm->tm_mday, tm->tm_mon, tm->tm_year,
1759                 tm->tm_hour, tm->tm_min, tm->tm_sec);
1760
1761         return 0;
1762 }
1763
1764 static int pcf50633_rtc_set_time(struct device *dev, struct rtc_time *tm)
1765 {
1766         struct i2c_client *client = to_i2c_client(dev);
1767         struct pcf50633_data *pcf = i2c_get_clientdata(client);
1768         struct pcf50633_time pcf_tm;
1769         int ret;
1770
1771         dev_dbg(dev, "RTC_TIME: %u.%u.%u %u:%u:%u\n",
1772                 tm->tm_mday, tm->tm_mon, tm->tm_year,
1773                 tm->tm_hour, tm->tm_min, tm->tm_sec);
1774         rtc2pcf_time(&pcf_tm, tm);
1775         dev_dbg(dev, "PCF_TIME: %02x.%02x.%02x %02x:%02x:%02x\n",
1776                 pcf_tm.time[PCF50633_TI_DAY],
1777                 pcf_tm.time[PCF50633_TI_MONTH],
1778                 pcf_tm.time[PCF50633_TI_YEAR],
1779                 pcf_tm.time[PCF50633_TI_HOUR],
1780                 pcf_tm.time[PCF50633_TI_MIN],
1781                 pcf_tm.time[PCF50633_TI_SEC]);
1782
1783         mutex_lock(&pcf->lock);
1784         /* FIXME: disable second interrupt */
1785
1786         ret = i2c_smbus_write_i2c_block_data(&pcf->client,
1787                                              PCF50633_REG_RTCSC,
1788                                              PCF50633_TI_EXTENT,
1789                                              &pcf_tm.time[0]);
1790         if (ret)
1791                 dev_err(dev, "Failed to set time %d\n", ret);
1792
1793         /* FIXME: re-enable second interrupt */
1794         mutex_unlock(&pcf->lock);
1795
1796         return 0;
1797 }
1798
1799 static int pcf50633_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
1800 {
1801         struct i2c_client *client = to_i2c_client(dev);
1802         struct pcf50633_data *pcf = i2c_get_clientdata(client);
1803         struct pcf50633_time pcf_tm;
1804         int ret;
1805
1806         mutex_lock(&pcf->lock);
1807
1808         alrm->enabled =
1809              __reg_read(pcf, PCF50633_REG_INT1M) & PCF50633_INT1_ALARM ? 0 : 1;
1810
1811         ret = i2c_smbus_read_i2c_block_data(&pcf->client,
1812                                             PCF50633_REG_RTCSCA,
1813                                             PCF50633_TI_EXTENT,
1814                                             &pcf_tm.time[0]);
1815         if (ret != PCF50633_TI_EXTENT)
1816                 dev_err(dev, "Failed to read Alarm time :-(\n");
1817
1818         mutex_unlock(&pcf->lock);
1819
1820         pcf2rtc_time(&alrm->time, &pcf_tm);
1821
1822         return 0;
1823 }
1824
1825 static int pcf50633_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
1826 {
1827         struct i2c_client *client = to_i2c_client(dev);
1828         struct pcf50633_data *pcf = i2c_get_clientdata(client);
1829         struct pcf50633_time pcf_tm;
1830         u_int8_t irqmask;
1831         int ret;
1832
1833         rtc2pcf_time(&pcf_tm, &alrm->time);
1834
1835         mutex_lock(&pcf->lock);
1836
1837         /* disable alarm interrupt */
1838         irqmask = __reg_read(pcf, PCF50633_REG_INT1M);
1839         irqmask |= PCF50633_INT1_ALARM;
1840         __reg_write(pcf, PCF50633_REG_INT1M, irqmask);
1841
1842         ret = i2c_smbus_write_i2c_block_data(&pcf->client,
1843                                              PCF50633_REG_RTCSCA,
1844                                              PCF50633_TI_EXTENT,
1845                                              &pcf_tm.time[0]);
1846         if (ret)
1847                 dev_err(dev, "Failed to write alarm time :-( %d\n", ret);
1848
1849         if (alrm->enabled) {
1850                 /* (re-)enaable alarm interrupt */
1851                 irqmask = __reg_read(pcf, PCF50633_REG_INT1M);
1852                 irqmask &= ~PCF50633_INT1_ALARM;
1853                 __reg_write(pcf, PCF50633_REG_INT1M, irqmask);
1854         }
1855
1856         mutex_unlock(&pcf->lock);
1857
1858         /* FIXME */
1859         return 0;
1860 }
1861
1862 static struct rtc_class_ops pcf50633_rtc_ops = {
1863         .ioctl          = pcf50633_rtc_ioctl,
1864         .read_time      = pcf50633_rtc_read_time,
1865         .set_time       = pcf50633_rtc_set_time,
1866         .read_alarm     = pcf50633_rtc_read_alarm,
1867         .set_alarm      = pcf50633_rtc_set_alarm,
1868 };
1869
1870 /***********************************************************************
1871  * Backlight device
1872  ***********************************************************************/
1873
1874 static int pcf50633bl_get_intensity(struct backlight_device *bd)
1875 {
1876         struct pcf50633_data *pcf = bl_get_data(bd);
1877         int intensity = reg_read(pcf, PCF50633_REG_LEDOUT);
1878
1879         return intensity & 0x3f;
1880 }
1881
1882 static int __pcf50633bl_set_intensity(struct pcf50633_data *pcf, int intensity)
1883 {
1884         int old_intensity = reg_read(pcf, PCF50633_REG_LEDOUT);
1885         u_int8_t ledena = 2;
1886         int ret;
1887
1888         if (!(reg_read(pcf, PCF50633_REG_LEDENA) & 1))
1889                 old_intensity = 0;
1890
1891         if ((pcf->backlight->props.power != FB_BLANK_UNBLANK) ||
1892             (pcf->backlight->props.fb_blank != FB_BLANK_UNBLANK))
1893                 intensity = 0;
1894
1895         /*
1896          * The PCF50633 cannot handle LEDOUT = 0 (datasheet p60)
1897          * if seen, you have to re-enable the LED unit
1898          */
1899
1900         if (intensity != 0 && old_intensity == 0) {
1901                 ledena = reg_read(pcf, PCF50633_REG_LEDENA);
1902                 reg_write(pcf, PCF50633_REG_LEDENA, 0x00);
1903         }
1904
1905         if (!intensity) /* illegal to set LEDOUT to 0 */
1906                 ret = reg_set_bit_mask(pcf, PCF50633_REG_LEDOUT, 0x3f, 2);
1907         else
1908                 ret = reg_set_bit_mask(pcf, PCF50633_REG_LEDOUT, 0x3f,
1909                                intensity);
1910
1911         if (intensity != 0 && old_intensity == 0)
1912                 reg_write(pcf, PCF50633_REG_LEDENA, ledena);
1913
1914         return ret;
1915 }
1916
1917 static int pcf50633bl_set_intensity(struct backlight_device *bd)
1918 {
1919         struct pcf50633_data *pcf = bl_get_data(bd);
1920         int intensity = bd->props.brightness;
1921
1922         if ((bd->props.power != FB_BLANK_UNBLANK) ||
1923             (bd->props.fb_blank != FB_BLANK_UNBLANK))
1924                 intensity = 0;
1925
1926         return __pcf50633bl_set_intensity(pcf, intensity);
1927 }
1928
1929 static struct backlight_ops pcf50633bl_ops = {
1930         .get_brightness = pcf50633bl_get_intensity,
1931         .update_status  = pcf50633bl_set_intensity,
1932 };
1933
1934 /*
1935  * Charger type
1936  */
1937
1938 static ssize_t show_charger_type(struct device *dev,
1939                                  struct device_attribute *attr, char *buf)
1940 {
1941         struct i2c_client *client = to_i2c_client(dev);
1942         struct pcf50633_data *pcf = i2c_get_clientdata(client);
1943         static const char *names_charger_type[] = {
1944                 [CHARGER_TYPE_NONE]     = "none",
1945                 [CHARGER_TYPE_HOSTUSB]  = "host/500mA usb",
1946                 [CHARGER_TYPE_1A]       = "charger 1A",
1947         };
1948         static const char *names_charger_modes[] = {
1949                 [PCF50633_MBCC7_USB_1000mA]     = "1A",
1950                 [PCF50633_MBCC7_USB_500mA]      = "500mA",
1951                 [PCF50633_MBCC7_USB_100mA]      = "100mA",
1952                 [PCF50633_MBCC7_USB_SUSPEND]    = "suspend",
1953         };
1954         int mode = reg_read(pcf, PCF50633_REG_MBCC7) & PCF56033_MBCC7_USB_MASK;
1955
1956         return sprintf(buf, "%s mode %s\n",
1957                             names_charger_type[pcf->charger_type],
1958                             names_charger_modes[mode]);
1959 }
1960
1961 static DEVICE_ATTR(charger_type, 0444, show_charger_type, NULL);
1962
1963 static ssize_t force_usb_limit_dangerous(struct device *dev,
1964                    struct device_attribute *attr, const char *buf, size_t count)
1965 {
1966         struct i2c_client *client = to_i2c_client(dev);
1967         struct pcf50633_data *pcf = i2c_get_clientdata(client);
1968         int ma = simple_strtoul(buf, NULL, 10);
1969
1970         pcf50633_usb_curlim_set(pcf, ma);
1971         return count;
1972 }
1973
1974 static DEVICE_ATTR(force_usb_limit_dangerous, 0600,
1975                                                NULL, force_usb_limit_dangerous);
1976
1977 /*
1978  * Charger adc
1979  */
1980
1981 static ssize_t show_charger_adc(struct device *dev,
1982                                 struct device_attribute *attr, char *buf)
1983 {
1984         struct i2c_client *client = to_i2c_client(dev);
1985         struct pcf50633_data *pcf = i2c_get_clientdata(client);
1986
1987         return sprintf(buf, "%d\n", pcf->charger_adc_result_raw);
1988 }
1989
1990 static DEVICE_ATTR(charger_adc, 0444, show_charger_adc, NULL);
1991
1992 /*
1993  * Dump regs
1994  */
1995
1996 static ssize_t show_dump_regs(struct device *dev, struct device_attribute *attr,
1997                             char *buf)
1998 {
1999         struct i2c_client *client = to_i2c_client(dev);
2000         struct pcf50633_data *pcf = i2c_get_clientdata(client);
2001         u8 dump[16];
2002         int n, n1, idx = 0;
2003         char *buf1 = buf;
2004         static u8 address_no_read[] = { /* must be ascending */
2005                 PCF50633_REG_INT1,
2006                 PCF50633_REG_INT2,
2007                 PCF50633_REG_INT3,
2008                 PCF50633_REG_INT4,
2009                 PCF50633_REG_INT5,
2010                 0 /* terminator */
2011         };
2012
2013         for (n = 0; n < 256; n += sizeof(dump)) {
2014
2015                 for (n1 = 0; n1 < sizeof(dump); n1++)
2016                         if (n == address_no_read[idx]) {
2017                                 idx++;
2018                                 dump[n1] = 0x00;
2019                         } else
2020                                 dump[n1] = reg_read(pcf, n + n1);
2021
2022                 hex_dump_to_buffer(dump, sizeof(dump), 16, 1, buf1, 128, 0);
2023                 buf1 += strlen(buf1);
2024                 *buf1++ = '\n';
2025                 *buf1 = '\0';
2026         }
2027
2028         return buf1 - buf;
2029 }
2030
2031 static DEVICE_ATTR(dump_regs, 0400, show_dump_regs, NULL);
2032
2033
2034 /***********************************************************************
2035  * Driver initialization
2036  ***********************************************************************/
2037
2038 /*
2039  * CARE!  This table is modified at runtime!
2040  */
2041 static struct attribute *pcf_sysfs_entries[] = {
2042         &dev_attr_voltage_auto.attr,
2043         &dev_attr_voltage_down1.attr,
2044         &dev_attr_voltage_down2.attr,
2045         &dev_attr_voltage_memldo.attr,
2046         &dev_attr_voltage_ldo1.attr,
2047         &dev_attr_voltage_ldo2.attr,
2048         &dev_attr_voltage_ldo3.attr,
2049         &dev_attr_voltage_ldo4.attr,
2050         &dev_attr_voltage_ldo5.attr,
2051         &dev_attr_voltage_ldo6.attr,
2052         &dev_attr_voltage_hcldo.attr,
2053         &dev_attr_charger_type.attr,
2054         &dev_attr_force_usb_limit_dangerous.attr,
2055         &dev_attr_charger_adc.attr,
2056         &dev_attr_dump_regs.attr,
2057         NULL, /* going to add things at this point! */
2058         NULL,
2059         NULL,
2060         NULL,
2061         NULL,
2062         NULL,
2063         NULL,
2064 };
2065
2066 static struct attribute_group pcf_attr_group = {
2067         .name   = NULL,                 /* put in device directory */
2068         .attrs  = pcf_sysfs_entries,
2069 };
2070
2071 static void populate_sysfs_group(struct pcf50633_data *pcf)
2072 {
2073         int i = 0;
2074         struct attribute **attr;
2075
2076         for (attr = pcf_sysfs_entries; *attr; attr++)
2077                 i++;
2078
2079         if (pcf->pdata->used_features & PCF50633_FEAT_MBC) {
2080                 pcf_sysfs_entries[i++] = &dev_attr_chgstate.attr;
2081                 pcf_sysfs_entries[i++] = &dev_attr_chgmode.attr;
2082                 pcf_sysfs_entries[i++] = &dev_attr_usb_curlim.attr;
2083         }
2084
2085         if (pcf->pdata->used_features & PCF50633_FEAT_CHGCUR)
2086                 pcf_sysfs_entries[i++] = &dev_attr_chgcur.attr;
2087
2088         if (pcf->pdata->used_features & PCF50633_FEAT_BATVOLT)
2089                 pcf_sysfs_entries[i++] = &dev_attr_battvolt.attr;
2090
2091         if (pcf->pdata->used_features & PCF50633_FEAT_BATTEMP)
2092                 pcf_sysfs_entries[i++] = &dev_attr_battemp.attr;
2093
2094 }
2095
2096 static int pcf50633_detect(struct i2c_adapter *adapter, int address, int kind)
2097 {
2098         struct i2c_client *new_client;
2099         struct pcf50633_data *pcf;
2100         int err = 0;
2101         int irq;
2102
2103         DEBUGP("entering\n");
2104         if (!pcf50633_pdev) {
2105                 printk(KERN_ERR "pcf50633: driver needs a platform_device!\n");
2106                 return -EIO;
2107         }
2108
2109         irq = platform_get_irq(pcf50633_pdev, 0);
2110         if (irq < 0) {
2111                 dev_err(&pcf50633_pdev->dev, "no irq in platform resources!\n");
2112                 return -EIO;
2113         }
2114
2115         /* At the moment, we only support one PCF50633 in a system */
2116         if (pcf50633_global) {
2117                 dev_err(&pcf50633_pdev->dev,
2118                         "currently only one chip supported\n");
2119                 return -EBUSY;
2120         }
2121
2122         pcf = kzalloc(sizeof(*pcf), GFP_KERNEL);
2123         if (!pcf)
2124                 return -ENOMEM;
2125
2126         mutex_init(&pcf->lock);
2127         mutex_init(&pcf->working_lock);
2128         mutex_init(&pcf->working_lock_nobat);
2129         mutex_init(&pcf->working_lock_usb_curlimit);
2130         INIT_WORK(&pcf->work, pcf50633_work);
2131         INIT_WORK(&pcf->work_nobat, pcf50633_work_nobat);
2132         INIT_WORK(&pcf->work_usb_curlimit, pcf50633_work_usbcurlim);
2133         pcf->irq = irq;
2134         pcf->working = 0;
2135         pcf->onkey_seconds = -1;
2136         pcf->pdata = pcf50633_pdev->dev.platform_data;
2137
2138         new_client = &pcf->client;
2139         i2c_set_clientdata(new_client, pcf);
2140         new_client->addr = address;
2141         new_client->adapter = adapter;
2142         new_client->driver = &pcf50633_driver;
2143         new_client->flags = 0;
2144         strlcpy(new_client->name, "pcf50633", I2C_NAME_SIZE);
2145
2146         /* now we try to detect the chip */
2147
2148         /* register with i2c core */
2149         if ((err = i2c_attach_client(new_client))) {
2150                 dev_err(&new_client->dev,
2151                         "error during i2c_attach_client()\n");
2152                 goto exit_free;
2153         }
2154
2155         pcf50633_global = pcf;
2156
2157         init_resume_dependency_list(&pcf->resume_dependency);
2158
2159         populate_sysfs_group(pcf);
2160
2161         err = sysfs_create_group(&new_client->dev.kobj, &pcf_attr_group);
2162         if (err) {
2163                 dev_err(&new_client->dev, "error creating sysfs group\n");
2164                 goto exit_detach;
2165         }
2166
2167         /* create virtual charger 'device' */
2168
2169         /* register power off handler with core power management */
2170         pm_power_off = &pcf50633_go_standby;
2171
2172         pcf->input_dev = input_allocate_device();
2173         if (!pcf->input_dev)
2174                 goto exit_sysfs;
2175
2176         pcf->input_dev->name = "GTA02 PMU events";
2177         pcf->input_dev->phys = "FIXME";
2178         pcf->input_dev->id.bustype = BUS_I2C;
2179
2180         pcf->input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_PWR);
2181         set_bit(KEY_POWER, pcf->input_dev->keybit);
2182         set_bit(KEY_POWER2, pcf->input_dev->keybit);
2183         set_bit(KEY_BATTERY, pcf->input_dev->keybit);
2184
2185         err = input_register_device(pcf->input_dev);
2186         if (err)
2187                 goto exit_sysfs;
2188
2189         /* configure interrupt mask */
2190
2191         /* we want SECOND to kick for the coldplug initialisation */
2192         reg_write(pcf, PCF50633_REG_INT1M, 0x00);
2193         reg_write(pcf, PCF50633_REG_INT2M, 0x00);
2194         reg_write(pcf, PCF50633_REG_INT3M, 0x00);
2195         reg_write(pcf, PCF50633_REG_INT4M, 0x00);
2196         reg_write(pcf, PCF50633_REG_INT5M, 0x00);
2197
2198         err = request_irq(irq, pcf50633_irq, IRQF_TRIGGER_FALLING,
2199                           "pcf50633", pcf);
2200         if (err < 0)
2201                 goto exit_input;
2202
2203         if (enable_irq_wake(irq) < 0)
2204                 dev_err(&new_client->dev, "IRQ %u cannot be enabled as wake-up"
2205                         "source in this hardware revision!", irq);
2206
2207         if (pcf->pdata->used_features & PCF50633_FEAT_RTC) {
2208                 pcf->rtc = rtc_device_register("pcf50633", &new_client->dev,
2209                                                 &pcf50633_rtc_ops, THIS_MODULE);
2210                 if (IS_ERR(pcf->rtc)) {
2211                         err = PTR_ERR(pcf->rtc);
2212                         goto exit_irq;
2213                 }
2214         }
2215
2216         if (pcf->pdata->used_features & PCF50633_FEAT_PWM_BL) {
2217                 pcf->backlight = backlight_device_register("pcf50633-bl",
2218                                                             &new_client->dev,
2219                                                             pcf,
2220                                                             &pcf50633bl_ops);
2221                 if (!pcf->backlight)
2222                         goto exit_rtc;
2223                 /* FIXME: are we sure we want default == off? */
2224                 pcf->backlight->props.max_brightness = 0x3f;
2225                 pcf->backlight->props.power = FB_BLANK_UNBLANK;
2226                 pcf->backlight->props.fb_blank = FB_BLANK_UNBLANK;
2227                 pcf->backlight->props.brightness =
2228                                         pcf->backlight->props.max_brightness;
2229                 backlight_update_status(pcf->backlight);
2230         }
2231
2232         pcf->probe_completed = 1;
2233
2234         if (pcf->pdata->flag_use_apm_emulation)
2235                 apm_get_power_status = pcf50633_get_power_status;
2236
2237         /* if platform was interested, give him a chance to register
2238          * platform devices that switch power with us as the parent
2239          * at registration time -- ensures suspend / resume ordering
2240          */
2241         if (pcf->pdata->attach_child_devices)
2242                 (pcf->pdata->attach_child_devices)(&new_client->dev);
2243
2244         return 0;
2245 exit_rtc:
2246         if (pcf->pdata->used_features & PCF50633_FEAT_RTC)
2247                 rtc_device_unregister(pcf50633_global->rtc);
2248 exit_irq:
2249         free_irq(pcf50633_global->irq, pcf50633_global);
2250 exit_input:
2251         input_unregister_device(pcf->input_dev);
2252 exit_sysfs:
2253         pm_power_off = NULL;
2254         sysfs_remove_group(&new_client->dev.kobj, &pcf_attr_group);
2255 exit_detach:
2256         i2c_detach_client(new_client);
2257 exit_free:
2258         kfree(pcf);
2259         pcf50633_global = NULL;
2260         return err;
2261 }
2262
2263 static int pcf50633_attach_adapter(struct i2c_adapter *adapter)
2264 {
2265         DEBUGP("entering, calling i2c_probe\n");
2266         return i2c_probe(adapter, &addr_data, &pcf50633_detect);
2267 }
2268
2269 static int pcf50633_detach_client(struct i2c_client *client)
2270 {
2271         struct pcf50633_data *pcf = i2c_get_clientdata(client);
2272
2273         DEBUGP("entering\n");
2274
2275         apm_get_power_status = NULL;
2276
2277         free_irq(pcf->irq, pcf);
2278
2279         input_unregister_device(pcf->input_dev);
2280
2281         if (pcf->pdata->used_features & PCF50633_FEAT_PWM_BL)
2282                 backlight_device_unregister(pcf->backlight);
2283
2284         if (pcf->pdata->used_features & PCF50633_FEAT_RTC)
2285                 rtc_device_unregister(pcf->rtc);
2286
2287         sysfs_remove_group(&client->dev.kobj, &pcf_attr_group);
2288
2289         pm_power_off = NULL;
2290
2291         kfree(pcf);
2292
2293         return 0;
2294 }
2295
2296 /* you're going to need >300 bytes in buf */
2297
2298 int pcf50633_report_resumers(struct pcf50633_data *pcf, char *buf)
2299 {
2300         static char *int_names[] = {
2301                 "adpins",
2302                 "adprem",
2303                 "usbins",
2304                 "usbrem",
2305                 NULL,
2306                 NULL,
2307                 "rtcalarm",
2308                 "second",
2309
2310                 "onkeyr",
2311                 "onkeyf",
2312                 "exton1r",
2313                 "exton1f",
2314                 "exton2r",
2315                 "exton2f",
2316                 "exton3r",
2317                 "exton3f",
2318
2319                 "batfull",
2320                 "chghalt",
2321                 "thlimon",
2322                 "thlimoff",
2323                 "usblimon",
2324                 "usblimoff",
2325                 "adcrdy",
2326                 "onkey1s",
2327
2328                 "lowsys",
2329                 "lowbat",
2330                 "hightmp",
2331                 "autopwrfail",
2332                 "dwn1pwrfail",
2333                 "dwn2pwrfail",
2334                 "ledpwrfail",
2335                 "ledovp",
2336
2337                 "ldo1pwrfail",
2338                 "ldo2pwrfail",
2339                 "ldo3pwrfail",
2340                 "ldo4pwrfail",
2341                 "ldo5pwrfail",
2342                 "ldo6pwrfail",
2343                 "hcidopwrfail",
2344                 "hcidoovl"
2345         };
2346         char *end = buf;
2347         int n;
2348
2349         for (n = 0; n < ARRAY_SIZE(int_names); n++)
2350                 if (int_names[n]) {
2351                         if (pcf->pcfirq_resume[n >> 3] & (1 >> (n & 7)))
2352                                 end += sprintf(end, "  * %s\n", int_names[n]);
2353                         else
2354                                 end += sprintf(end, "    %s\n", int_names[n]);
2355                 }
2356         
2357         return end - buf;
2358 }
2359
2360
2361 #ifdef CONFIG_PM
2362
2363 /*
2364  * we need to export this because pcf50633_data is kept opaque
2365  */
2366
2367 void pcf50633_register_resume_dependency(struct pcf50633_data *pcf,
2368                                         struct resume_dependency *dep)
2369 {
2370         register_resume_dependency(&pcf->resume_dependency, dep);
2371 }
2372 EXPORT_SYMBOL_GPL(pcf50633_register_resume_dependency);
2373
2374
2375 static int pcf50633_suspend(struct device *dev, pm_message_t state)
2376 {
2377         struct i2c_client *client = to_i2c_client(dev);
2378         struct pcf50633_data *pcf = i2c_get_clientdata(client);
2379         int i;
2380         int ret;
2381         u_int8_t tmp;
2382         u_int8_t res[5];
2383
2384         dev_err(dev, "pcf50633_suspend\n");
2385
2386         /* we suspend once (!) as late as possible in the suspend sequencing */
2387
2388         if ((state.event != PM_EVENT_SUSPEND) ||
2389             (pcf->suspend_state != PCF50633_SS_RUNNING))
2390                 return -EBUSY;
2391
2392         /* The general idea is to power down all unused power supplies,
2393          * and then mask all PCF50633 interrupt sources but EXTONR, ONKEYF
2394          * and ALARM */
2395
2396         mutex_lock(&pcf->lock);
2397
2398         pcf->suspend_state = PCF50633_SS_STARTING_SUSPEND;
2399
2400         /* we are not going to service any further interrupts until we
2401          * resume.  If the IRQ workqueue is still pending in the background,
2402          * it will bail when it sees we set suspend state above
2403          */
2404
2405         disable_irq(pcf->irq);
2406
2407         /* Save all registers that don't "survive" standby state */
2408         pcf->standby_regs.ooctim2 = __reg_read(pcf, PCF50633_REG_OOCTIM2);
2409
2410         ret = i2c_smbus_read_i2c_block_data(&pcf->client,
2411                                             PCF50633_REG_AUTOOUT,
2412                                             sizeof(pcf->standby_regs.misc),
2413                                             &pcf->standby_regs.misc[0]);
2414         if (ret != sizeof(pcf->standby_regs.misc))
2415                 dev_err(dev, "Failed to save misc levels and enables :-(\n");
2416
2417         /* regulator voltages and enable states */
2418         ret = i2c_smbus_read_i2c_block_data(&pcf->client,
2419                                             PCF50633_REG_LDO1OUT,
2420                                             sizeof(pcf->standby_regs.ldo),
2421                                             &pcf->standby_regs.ldo[0]);
2422         if (ret != sizeof(pcf->standby_regs.ldo))
2423                 dev_err(dev, "Failed to save LDO levels and enables :-(\n");
2424
2425         /* switch off power supplies that are not needed during suspend */
2426         for (i = 0; i < __NUM_PCF50633_REGULATORS; i++) {
2427                 if ((pcf->pdata->rails[i].flags & PMU_VRAIL_F_SUSPEND_ON))
2428                         continue;
2429
2430                 /* we can save ourselves the read part of a read-modify-write
2431                  * here because we captured all these already
2432                  */
2433                 if (i < 4)
2434                         tmp = pcf->standby_regs.misc[i * 4 + 1];
2435                 else
2436                         tmp = pcf->standby_regs.ldo[(i - 4) * 2 + 1];
2437
2438                 dev_dbg(dev, "disabling reg %s by setting ENA %d to 0x%02X\n",
2439                               pcf->pdata->rails[i].name,
2440                               regulator_registers[i] + 1, tmp & 0xfe);
2441
2442                 /* associated enable is always +1 from OUT reg */
2443                 __reg_write(pcf, regulator_registers[i] + 1, tmp & 0xfe);
2444         }
2445
2446         /* turn off the backlight */
2447         __reg_write(pcf, PCF50633_REG_LEDDIM, 0);
2448         __reg_write(pcf, PCF50633_REG_LEDOUT, 2);
2449         __reg_write(pcf, PCF50633_REG_LEDENA, 0x00);
2450
2451         /* set interrupt masks so only those sources we want to wake
2452          * us are able to
2453          */
2454         for (i = 0; i < 5; i++)
2455                 res[i] = ~pcf->pdata->resumers[i];
2456
2457         ret = i2c_smbus_write_i2c_block_data(&pcf->client,
2458                                              PCF50633_REG_INT1M,
2459                                              5, &res[0]);
2460         if (ret)
2461                 dev_err(dev, "Failed to set wake masks :-( %d\n", ret);
2462
2463         pcf->suspend_state = PCF50633_SS_COMPLETED_SUSPEND;
2464
2465         mutex_unlock(&pcf->lock);
2466
2467         return 0;
2468 }
2469
2470
2471 int pcf50633_ready(struct pcf50633_data *pcf)
2472 {
2473         if (!pcf)
2474                 return -EACCES;
2475
2476         if ((pcf->suspend_state != PCF50633_SS_RUNNING) &&
2477             (pcf->suspend_state < PCF50633_SS_COMPLETED_RESUME))
2478                 return -EBUSY;
2479
2480         return 0;
2481 }
2482 EXPORT_SYMBOL_GPL(pcf50633_ready);
2483
2484 int pcf50633_wait_for_ready(struct pcf50633_data *pcf, int timeout_ms,
2485                                                                 char *name)
2486 {
2487         /* so we always go once */
2488         timeout_ms += 5;
2489
2490         while ((timeout_ms >= 5) && (pcf50633_ready(pcf))) {
2491                 timeout_ms -= 5; /* well, it isn't very accurate, but OK */
2492                 msleep(5);
2493         }
2494
2495         if (timeout_ms < 5) {
2496                 printk(KERN_ERR"pcf50633_wait_for_ready: "
2497                                         "%s BAILING on timeout\n", name);
2498                 return -EBUSY;
2499         }
2500
2501         return 0;
2502 }
2503 EXPORT_SYMBOL_GPL(pcf50633_wait_for_ready);
2504
2505 /*
2506  * if backlight resume is selected to be deferred by platform, then it
2507  * can call this to finally reset backlight status (after LCM is resumed
2508  * for example
2509  */
2510
2511 void pcf50633_backlight_resume(struct pcf50633_data *pcf)
2512 {
2513         dev_dbg(&pcf->client.dev, "pcf50633_backlight_resume\n");
2514
2515         /* platform defines resume ramp speed */
2516         reg_write(pcf, PCF50633_REG_LEDDIM,
2517                                        pcf->pdata->resume_backlight_ramp_speed);
2518
2519         __pcf50633bl_set_intensity(pcf, pcf->backlight->props.brightness);
2520 }
2521 EXPORT_SYMBOL_GPL(pcf50633_backlight_resume);
2522
2523
2524 static int pcf50633_resume(struct device *dev)
2525 {
2526         struct i2c_client *client = to_i2c_client(dev);
2527         struct pcf50633_data *pcf = i2c_get_clientdata(client);
2528         int ret;
2529         u8 res[5];
2530         u8 misc[PCF50633_REG_LEDDIM - PCF50633_REG_AUTOOUT + 1];
2531
2532         dev_dbg(dev, "pcf50633_resume suspended on entry = %d\n",
2533                                                  (int)pcf->suspend_state);
2534         mutex_lock(&pcf->lock);
2535
2536         pcf->suspend_state = PCF50633_SS_STARTING_RESUME;
2537
2538         /* these guys get reset while pcf50633 is suspend state, refresh */
2539
2540         __reg_write(pcf, PCF50633_REG_OOCTIM2, pcf->standby_regs.ooctim2);
2541
2542         memcpy(misc, pcf->standby_regs.misc, sizeof(pcf->standby_regs.misc));
2543
2544         if (pcf->pdata->defer_resume_backlight) {
2545                 misc[PCF50633_REG_LEDOUT - PCF50633_REG_AUTOOUT] = 1;
2546                 misc[PCF50633_REG_LEDENA - PCF50633_REG_AUTOOUT] = 0x20;
2547                 misc[PCF50633_REG_LEDCTL - PCF50633_REG_AUTOOUT] = 1;
2548                 misc[PCF50633_REG_LEDDIM - PCF50633_REG_AUTOOUT] = 1;
2549         }
2550
2551         /* regulator voltages and enable states */
2552         ret = i2c_smbus_write_i2c_block_data(&pcf->client,
2553                                              PCF50633_REG_AUTOOUT,
2554                                              sizeof(misc),
2555                                              &misc[0]);
2556         if (ret)
2557                 dev_err(dev, "Failed to restore misc :-( %d\n", ret);
2558
2559         /* platform can choose to defer backlight bringup */
2560         if (!pcf->pdata->defer_resume_backlight)
2561                 pcf50633_backlight_resume(pcf);
2562
2563         /* regulator voltages and enable states */
2564         ret = i2c_smbus_write_i2c_block_data(&pcf->client,
2565                                              PCF50633_REG_LDO1OUT,
2566                                              sizeof(pcf->standby_regs.ldo),
2567                                              &pcf->standby_regs.ldo[0]);
2568         if (ret)
2569                 dev_err(dev, "Failed to restore LDOs :-( %d\n", ret);
2570
2571         memset(res, 0, sizeof(res));
2572         /* not interested in second on resume */
2573         res[0] = PCF50633_INT1_SECOND;
2574         ret = i2c_smbus_write_i2c_block_data(&pcf->client,
2575                                              PCF50633_REG_INT1M,
2576                                              5, &res[0]);
2577         if (ret)
2578                 dev_err(dev, "Failed to set int masks :-( %d\n", ret);
2579
2580         pcf->suspend_state = PCF50633_SS_COMPLETED_RESUME;
2581
2582         enable_irq(pcf->irq);
2583
2584         mutex_unlock(&pcf->lock);
2585
2586         /* gratuitous call to PCF work function, in the case that the PCF
2587          * interrupt edge was missed during resume, this forces the pending
2588          * register clear and lifts the interrupt back high again.  In the
2589          * case nothing is waiting for service, no harm done.
2590          */
2591
2592         get_device(&pcf->client.dev);
2593         pcf50633_work(&pcf->work);
2594
2595         callback_all_resume_dependencies(&pcf->resume_dependency);
2596
2597         return 0;
2598 }
2599 #else
2600 #define pcf50633_suspend NULL
2601 #define pcf50633_resume NULL
2602 #endif
2603
2604 static struct i2c_driver pcf50633_driver = {
2605         .driver = {
2606                 .name   = "pcf50633",
2607                 .suspend= pcf50633_suspend,
2608                 .resume = pcf50633_resume,
2609         },
2610         .id             = I2C_DRIVERID_PCF50633,
2611         .attach_adapter = pcf50633_attach_adapter,
2612         .detach_client  = pcf50633_detach_client,
2613 };
2614
2615 /* we have this purely to capture an early indication that we are coming out
2616  * of suspend, before our device resume got called; async interrupt service is
2617  * interested in this
2618  */
2619
2620 static int pcf50633_plat_resume(struct platform_device *pdev)
2621 {
2622         /* i2c_get_clientdata(to_i2c_client(&pdev->dev)) returns NULL at this
2623          * early resume time so we have to use pcf50633_global
2624          */
2625         pcf50633_global->suspend_state = PCF50633_SS_RESUMING_BUT_NOT_US_YET;
2626
2627         return 0;
2628 }
2629
2630 /* platform driver, since i2c devices don't have platform_data */
2631 static int __init pcf50633_plat_probe(struct platform_device *pdev)
2632 {
2633         struct pcf50633_platform_data *pdata = pdev->dev.platform_data;
2634
2635         if (!pdata)
2636                 return -ENODEV;
2637
2638         pcf50633_pdev = pdev;
2639
2640         return 0;
2641 }
2642
2643 static int pcf50633_plat_remove(struct platform_device *pdev)
2644 {
2645         return 0;
2646 }
2647
2648 static struct platform_driver pcf50633_plat_driver = {
2649         .probe  = pcf50633_plat_probe,
2650         .remove = pcf50633_plat_remove,
2651         .resume_early = pcf50633_plat_resume,
2652         .driver = {
2653                 .owner  = THIS_MODULE,
2654                 .name   = "pcf50633",
2655         },
2656 };
2657
2658 static int __init pcf50633_init(void)
2659 {
2660         int rc;
2661
2662         if (!(rc = platform_driver_register(&pcf50633_plat_driver)))
2663                 rc = i2c_add_driver(&pcf50633_driver);
2664
2665         return rc;
2666 }
2667
2668 static void pcf50633_exit(void)
2669 {
2670         i2c_del_driver(&pcf50633_driver);
2671         platform_driver_unregister(&pcf50633_plat_driver);
2672 }
2673
2674 MODULE_DESCRIPTION("I2C chip driver for NXP PCF50633 power management unit");
2675 MODULE_AUTHOR("Harald Welte <laforge@openmoko.org>");
2676 MODULE_LICENSE("GPL");
2677
2678 module_init(pcf50633_init);
2679 module_exit(pcf50633_exit);