Merge remote-tracking branch 'origin/upstream/linux-linaro-lsk-v3.10-android+android...
[firefly-linux-kernel-4.4.55.git] / drivers / mfd / ricoh619.c
1 /* 
2  * driver/mfd/ricoh619.c
3  *
4  * Core driver implementation to access RICOH RC5T619 power management chip.
5  *
6  * Copyright (C) 2012-2013 RICOH COMPANY,LTD
7  *
8  * Based on code
9  *      Copyright (C) 2011 NVIDIA Corporation
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful, but WITHOUT
17  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
19  * more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  *
24  */
25 /*#define DEBUG                 1*/
26 /*#define VERBOSE_DEBUG         1*/
27 #include <linux/interrupt.h>
28 #include <linux/irq.h>
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/slab.h>
33 #include <linux/gpio.h>
34 #include <linux/i2c.h>
35 #include <linux/mfd/core.h>
36 #include <linux/mfd/ricoh619.h>
37 #include <linux/power/ricoh619_battery.h>
38
39
40 struct ricoh619 *g_ricoh619;
41 struct sleep_control_data {
42         u8 reg_add;
43 };
44
45 #define SLEEP_INIT(_id, _reg)           \
46         [RICOH619_DS_##_id] = {.reg_add = _reg}
47
48 static struct sleep_control_data sleep_data[] = {
49         SLEEP_INIT(DC1, 0x16),
50         SLEEP_INIT(DC2, 0x17),
51         SLEEP_INIT(DC3, 0x18),
52         SLEEP_INIT(DC4, 0x19),
53         SLEEP_INIT(DC5, 0x1A),
54         SLEEP_INIT(LDO1, 0x1B),
55         SLEEP_INIT(LDO2, 0x1C),
56         SLEEP_INIT(LDO3, 0x1D),
57         SLEEP_INIT(LDO4, 0x1E),
58         SLEEP_INIT(LDO5, 0x1F),
59         SLEEP_INIT(LDO6, 0x20),
60         SLEEP_INIT(LDO7, 0x21),
61         SLEEP_INIT(LDO8, 0x22),
62         SLEEP_INIT(LDO9, 0x23),
63         SLEEP_INIT(LDO10, 0x24),
64         SLEEP_INIT(PSO0, 0x25),
65         SLEEP_INIT(PSO1, 0x26),
66         SLEEP_INIT(PSO2, 0x27),
67         SLEEP_INIT(PSO3, 0x28),
68         SLEEP_INIT(PSO4, 0x29),
69         SLEEP_INIT(LDORTC1, 0x2A),
70 };
71
72 static inline int __ricoh619_read(struct i2c_client *client,
73                                   u8 reg, uint8_t *val)
74 {
75         int ret;
76
77         ret = i2c_smbus_read_byte_data(client, reg);
78         if (ret < 0) {
79                 dev_err(&client->dev, "failed reading at 0x%02x\n", reg);
80                 return ret;
81         }
82
83         *val = (uint8_t)ret;
84         dev_dbg(&client->dev, "ricoh619: reg read  reg=%x, val=%x\n",
85                                 reg, *val);
86         return 0;
87 }
88
89 static inline int __ricoh619_bulk_reads(struct i2c_client *client, u8 reg,
90                                 int len, uint8_t *val)
91 {
92         int ret;
93         int i;
94
95         ret = i2c_smbus_read_i2c_block_data(client, reg, len, val);
96         if (ret < 0) {
97                 dev_err(&client->dev, "failed reading from 0x%02x\n", reg);
98                 return ret;
99         }
100         for (i = 0; i < len; ++i) {
101                 dev_dbg(&client->dev, "ricoh619: reg read  reg=%x, val=%x\n",
102                                 reg + i, *(val + i));
103         }
104         return 0;
105 }
106
107 static inline int __ricoh619_write(struct i2c_client *client,
108                                  u8 reg, uint8_t val)
109 {
110         int ret;
111
112         dev_dbg(&client->dev, "ricoh619: reg write  reg=%x, val=%x\n",
113                                 reg, val);
114         ret = i2c_smbus_write_byte_data(client, reg, val);
115         if (ret < 0) {
116                 dev_err(&client->dev, "failed writing 0x%02x to 0x%02x\n",
117                                 val, reg);
118                 return ret;
119         }
120
121         return 0;
122 }
123
124 static inline int __ricoh619_bulk_writes(struct i2c_client *client, u8 reg,
125                                   int len, uint8_t *val)
126 {
127         int ret;
128         int i;
129
130         for (i = 0; i < len; ++i) {
131                 dev_dbg(&client->dev, "ricoh619: reg write  reg=%x, val=%x\n",
132                                 reg + i, *(val + i));
133         }
134
135         ret = i2c_smbus_write_i2c_block_data(client, reg, len, val);
136         if (ret < 0) {
137                 dev_err(&client->dev, "failed writings to 0x%02x\n", reg);
138                 return ret;
139         }
140
141         return 0;
142 }
143
144 static inline int set_bank_ricoh619(struct device *dev, int bank)
145 {
146         struct ricoh619 *ricoh619 = dev_get_drvdata(dev);
147         int ret;
148
149         if (bank != (bank & 1))
150                 return -EINVAL;
151         if (bank == ricoh619->bank_num)
152                 return 0;
153         ret = __ricoh619_write(to_i2c_client(dev), RICOH619_REG_BANKSEL, bank);
154         if (!ret)
155                 ricoh619->bank_num = bank;
156
157         return ret;
158 }
159
160 int ricoh619_write(struct device *dev, u8 reg, uint8_t val)
161 {
162         struct ricoh619 *ricoh619 = dev_get_drvdata(dev);
163         int ret = 0;
164
165         mutex_lock(&ricoh619->io_lock);
166         ret = set_bank_ricoh619(dev, 0);
167         if( !ret )
168                 ret = __ricoh619_write(to_i2c_client(dev), reg, val);
169         mutex_unlock(&ricoh619->io_lock);
170
171         return ret;
172 }
173 EXPORT_SYMBOL_GPL(ricoh619_write);
174
175 int ricoh619_write_bank1(struct device *dev, u8 reg, uint8_t val)
176 {
177         struct ricoh619 *ricoh619 = dev_get_drvdata(dev);
178         int ret = 0;
179
180         mutex_lock(&ricoh619->io_lock);
181         ret = set_bank_ricoh619(dev, 1);
182         if( !ret ) 
183                 ret = __ricoh619_write(to_i2c_client(dev), reg, val);
184         mutex_unlock(&ricoh619->io_lock);
185
186         return ret;
187 }
188 EXPORT_SYMBOL_GPL(ricoh619_write_bank1);
189
190 int ricoh619_bulk_writes(struct device *dev, u8 reg, u8 len, uint8_t *val)
191 {
192         struct ricoh619 *ricoh619 = dev_get_drvdata(dev);
193         int ret = 0;
194
195         mutex_lock(&ricoh619->io_lock);
196         ret = set_bank_ricoh619(dev, 0);
197         if( !ret )
198                 ret = __ricoh619_bulk_writes(to_i2c_client(dev), reg, len, val);
199         mutex_unlock(&ricoh619->io_lock);
200
201         return ret;
202 }
203 EXPORT_SYMBOL_GPL(ricoh619_bulk_writes);
204
205 int ricoh619_bulk_writes_bank1(struct device *dev, u8 reg, u8 len, uint8_t *val)
206 {
207         struct ricoh619 *ricoh619 = dev_get_drvdata(dev);
208         int ret = 0;
209
210         mutex_lock(&ricoh619->io_lock);
211         ret = set_bank_ricoh619(dev, 1);
212         if( !ret ) 
213                 ret = __ricoh619_bulk_writes(to_i2c_client(dev), reg, len, val);
214         mutex_unlock(&ricoh619->io_lock);
215
216         return ret;
217 }
218 EXPORT_SYMBOL_GPL(ricoh619_bulk_writes_bank1);
219
220 int ricoh619_read(struct device *dev, u8 reg, uint8_t *val)
221 {
222         struct ricoh619 *ricoh619 = dev_get_drvdata(dev);
223         int ret = 0;
224
225         mutex_lock(&ricoh619->io_lock);
226         ret = set_bank_ricoh619(dev, 0);
227         if( !ret )
228                 ret = __ricoh619_read(to_i2c_client(dev), reg, val);
229         mutex_unlock(&ricoh619->io_lock);
230
231         return ret;
232 }
233 EXPORT_SYMBOL_GPL(ricoh619_read);
234
235 int ricoh619_read_bank1(struct device *dev, u8 reg, uint8_t *val)
236 {
237         struct ricoh619 *ricoh619 = dev_get_drvdata(dev);
238         int ret = 0;
239
240         mutex_lock(&ricoh619->io_lock);
241         ret = set_bank_ricoh619(dev, 1);
242         if( !ret )
243                 ret =  __ricoh619_read(to_i2c_client(dev), reg, val);
244         mutex_unlock(&ricoh619->io_lock);
245
246         return ret;
247 }
248
249 EXPORT_SYMBOL_GPL(ricoh619_read_bank1);
250
251 int ricoh619_bulk_reads(struct device *dev, u8 reg, u8 len, uint8_t *val)
252 {
253         struct ricoh619 *ricoh619 = dev_get_drvdata(dev);
254         int ret = 0;
255
256         mutex_lock(&ricoh619->io_lock);
257         ret = set_bank_ricoh619(dev, 0);
258         if( !ret ) 
259                 ret = __ricoh619_bulk_reads(to_i2c_client(dev), reg, len, val);
260         mutex_unlock(&ricoh619->io_lock);
261
262         return ret;
263 }
264 EXPORT_SYMBOL_GPL(ricoh619_bulk_reads);
265
266 int ricoh619_bulk_reads_bank1(struct device *dev, u8 reg, u8 len, uint8_t *val)
267 {
268         struct ricoh619 *ricoh619 = dev_get_drvdata(dev);
269         int ret = 0;
270
271         mutex_lock(&ricoh619->io_lock);
272         ret = set_bank_ricoh619(dev, 1);
273         if( !ret ) 
274                 ret = __ricoh619_bulk_reads(to_i2c_client(dev), reg, len, val);
275         mutex_unlock(&ricoh619->io_lock);
276
277         return ret;
278 }
279 EXPORT_SYMBOL_GPL(ricoh619_bulk_reads_bank1);
280
281 int ricoh619_set_bits(struct device *dev, u8 reg, uint8_t bit_mask)
282 {
283         struct ricoh619 *ricoh619 = dev_get_drvdata(dev);
284         uint8_t reg_val;
285         int ret = 0;
286
287         mutex_lock(&ricoh619->io_lock);
288         ret = set_bank_ricoh619(dev, 0);
289         if (!ret) {
290                 ret = __ricoh619_read(to_i2c_client(dev), reg, &reg_val);
291                 if (ret)
292                         goto out;
293
294                 if ((reg_val & bit_mask) != bit_mask) {
295                         reg_val |= bit_mask;
296                         ret = __ricoh619_write(to_i2c_client(dev), reg,
297                                                                  reg_val);
298                 }
299         }
300 out:
301         mutex_unlock(&ricoh619->io_lock);
302         return ret;
303 }
304 EXPORT_SYMBOL_GPL(ricoh619_set_bits);
305
306 int ricoh619_clr_bits(struct device *dev, u8 reg, uint8_t bit_mask)
307 {
308         struct ricoh619 *ricoh619 = dev_get_drvdata(dev);
309         uint8_t reg_val;
310         int ret = 0;
311
312         mutex_lock(&ricoh619->io_lock);
313         ret = set_bank_ricoh619(dev, 0);
314         if( !ret ){
315                 ret = __ricoh619_read(to_i2c_client(dev), reg, &reg_val);
316                 if (ret)
317                         goto out;
318
319                 if (reg_val & bit_mask) {
320                         reg_val &= ~bit_mask;
321                         ret = __ricoh619_write(to_i2c_client(dev), reg,
322                                                                  reg_val);
323                 }
324         }
325 out:
326         mutex_unlock(&ricoh619->io_lock);
327         return ret;
328 }
329 EXPORT_SYMBOL_GPL(ricoh619_clr_bits);
330
331 int ricoh619_update(struct device *dev, u8 reg, uint8_t val, uint8_t mask)
332 {
333         struct ricoh619 *ricoh619 = dev_get_drvdata(dev);
334         uint8_t reg_val;
335         int ret = 0;
336
337         mutex_lock(&ricoh619->io_lock);
338         ret = set_bank_ricoh619(dev, 0);
339         if( !ret ){
340                 ret = __ricoh619_read(ricoh619->client, reg, &reg_val);
341                 if (ret)
342                         goto out;
343
344                 if ((reg_val & mask) != val) {
345                         reg_val = (reg_val & ~mask) | (val & mask);
346                         ret = __ricoh619_write(ricoh619->client, reg, reg_val);
347                 }
348         }
349 out:
350         mutex_unlock(&ricoh619->io_lock);
351         return ret;
352 }
353 EXPORT_SYMBOL_GPL(ricoh619_update);
354
355 int ricoh619_update_bank1(struct device *dev, u8 reg, uint8_t val, uint8_t mask)
356 {
357         struct ricoh619 *ricoh619 = dev_get_drvdata(dev);
358         uint8_t reg_val;
359         int ret = 0;
360
361         mutex_lock(&ricoh619->io_lock);
362         ret = set_bank_ricoh619(dev, 1);
363         if( !ret ){
364                 ret = __ricoh619_read(ricoh619->client, reg, &reg_val);
365                 if (ret)
366                         goto out;
367
368                 if ((reg_val & mask) != val) {
369                         reg_val = (reg_val & ~mask) | (val & mask);
370                         ret = __ricoh619_write(ricoh619->client, reg, reg_val);
371                 }
372         }
373 out:
374         mutex_unlock(&ricoh619->io_lock);
375         return ret;
376 }
377
378 static struct i2c_client *ricoh619_i2c_client;
379 int ricoh619_power_off(void)
380 {
381         int ret;
382         uint8_t val;
383         int err = -1;
384         int status, charge_state;
385         struct ricoh619 *ricoh619 = g_ricoh619;
386
387         val = g_soc;
388         val &= 0x7f;
389         ricoh619_read(ricoh619->dev, 0xBD, &status);
390         charge_state = (status & 0x1F);
391 //      supply_state = ((status & 0xC0) >> 6);
392
393         ret = ricoh619_write(ricoh619->dev, RICOH619_PSWR, val);
394         if (ret < 0)
395                 dev_err(ricoh619->dev, "Error in writing PSWR_REG\n");
396
397         if (g_fg_on_mode == 0) {
398                 ret = ricoh619_clr_bits(ricoh619->dev,
399                                          RICOH619_FG_CTRL, 0x01);
400                 if (ret < 0)
401                         dev_err(ricoh619->dev, "Error in writing FG_CTRL\n");
402         }
403         
404         /* set rapid timer 300 min */
405         err = ricoh619_set_bits(ricoh619->dev, TIMSET_REG, 0x03);
406         if (err < 0)
407                 dev_err(ricoh619->dev, "Error in writing the TIMSET_Reg\n");
408   
409         ret = ricoh619_write(ricoh619->dev, RICOH619_INTC_INTEN, 0); 
410
411         if (!ricoh619_i2c_client)
412                 return -EINVAL;
413 //__ricoh618_write(ricoh618_i2c_client, RICOH618_PWR_REP_CNT, 0x0); //Not repeat power ON after power off(Power Off/N_OE)
414 //      __ricoh618_write(ricoh618_i2c_client, RICOH618_PWR_SLP_CNT, 0x1); //Power OFF
415         ret = ricoh619_clr_bits(ricoh619->dev,RICOH619_PWR_REP_CNT,(0x1<<0));//Not repeat power ON after power off(Power Off/N_OE)
416
417         if(( charge_state == CHG_STATE_CHG_TRICKLE)||( charge_state == CHG_STATE_CHG_RAPID))
418                  ricoh619_set_bits(ricoh619->dev, RICOH619_PWR_REP_CNT,(0x1<<0));//Power OFF
419         ret = ricoh619_set_bits(ricoh619->dev, RICOH619_PWR_SLP_CNT,(0x1<<0));//Power OFF
420         if (ret < 0) {
421                 printk("ricoh619 power off error!\n");
422                 return err;
423         }
424         return 0;
425 }
426 EXPORT_SYMBOL_GPL(ricoh619_power_off);
427
428 static int ricoh619_gpio_get(struct gpio_chip *gc, unsigned offset)
429 {
430         struct ricoh619 *ricoh619 = container_of(gc, struct ricoh619, gpio_chip);
431         uint8_t val;
432         int ret;
433
434         ret = ricoh619_read(ricoh619->dev, RICOH619_GPIO_MON_IOIN, &val);
435         if (ret < 0)
436                 return ret;
437
438         return ((val & (0x1 << offset)) != 0);
439 }
440
441 static void ricoh619_gpio_set(struct gpio_chip *gc, unsigned offset,
442                         int value)
443 {
444         struct ricoh619 *ricoh619 = container_of(gc, struct ricoh619, gpio_chip);
445         if (value)
446                 ricoh619_set_bits(ricoh619->dev, RICOH619_GPIO_IOOUT,
447                                                 1 << offset);
448         else
449                 ricoh619_clr_bits(ricoh619->dev, RICOH619_GPIO_IOOUT,
450                                                 1 << offset);
451 }
452
453 static int ricoh619_gpio_input(struct gpio_chip *gc, unsigned offset)
454 {
455         struct ricoh619 *ricoh619 = container_of(gc, struct ricoh619, gpio_chip);
456
457         return ricoh619_clr_bits(ricoh619->dev, RICOH619_GPIO_IOSEL,
458                                                 1 << offset);
459 }
460
461 static int ricoh619_gpio_output(struct gpio_chip *gc, unsigned offset,
462                                 int value)
463 {
464         struct ricoh619 *ricoh619 = container_of(gc, struct ricoh619, gpio_chip);
465
466         ricoh619_gpio_set(gc, offset, value);
467         return ricoh619_set_bits(ricoh619->dev, RICOH619_GPIO_IOSEL,
468                                                 1 << offset);
469 }
470
471 static int ricoh619_gpio_to_irq(struct gpio_chip *gc, unsigned off)
472 {
473         struct ricoh619 *ricoh619 = container_of(gc, struct ricoh619, gpio_chip);
474
475         if ((off >= 0) && (off < 8))
476                 return ricoh619->irq_base + RICOH619_IRQ_GPIO0 + off;
477
478         return -EIO;
479 }
480
481
482 static void __devinit ricoh619_gpio_init(struct ricoh619 *ricoh619,
483         struct ricoh619_platform_data *pdata)
484 {
485         int ret;
486         int i;
487         struct ricoh619_gpio_init_data *ginit;
488
489         if (pdata->gpio_base  <= 0)
490                 return;
491
492         for (i = 0; i < pdata->num_gpioinit_data; ++i) {
493                 ginit = &pdata->gpio_init_data[i];
494
495                 if (!ginit->init_apply)
496                         continue;
497
498                 if (ginit->output_mode_en) {
499                         /* GPIO output mode */
500                         if (ginit->output_val)
501                                 /* output H */
502                                 ret = ricoh619_set_bits(ricoh619->dev,
503                                         RICOH619_GPIO_IOOUT, 1 << i);
504                         else
505                                 /* output L */
506                                 ret = ricoh619_clr_bits(ricoh619->dev,
507                                         RICOH619_GPIO_IOOUT, 1 << i);
508                         if (!ret)
509                                 ret = ricoh619_set_bits(ricoh619->dev,
510                                         RICOH619_GPIO_IOSEL, 1 << i);
511                 } else
512                         /* GPIO input mode */
513                         ret = ricoh619_clr_bits(ricoh619->dev,
514                                         RICOH619_GPIO_IOSEL, 1 << i);
515
516                 /* if LED function enabled in OTP */
517                 if (ginit->led_mode) {
518                         /* LED Mode 1 */
519                         if (i == 0)     /* GP0 */
520                                 ret = ricoh619_set_bits(ricoh619->dev,
521                                          RICOH619_GPIO_LED_FUNC,
522                                          0x04 | (ginit->led_func & 0x03));
523                         if (i == 1)     /* GP1 */
524                                 ret = ricoh619_set_bits(ricoh619->dev,
525                                          RICOH619_GPIO_LED_FUNC,
526                                          0x40 | (ginit->led_func & 0x03) << 4);
527
528                 }
529
530
531                 if (ret < 0)
532                         dev_err(ricoh619->dev, "Gpio %d init "
533                                 "dir configuration failed: %d\n", i, ret);
534
535         }
536
537         ricoh619->gpio_chip.owner               = THIS_MODULE;
538         ricoh619->gpio_chip.label               = ricoh619->client->name;
539         ricoh619->gpio_chip.dev                 = ricoh619->dev;
540         ricoh619->gpio_chip.base                = pdata->gpio_base;
541         ricoh619->gpio_chip.ngpio               = RICOH619_NR_GPIO;
542         ricoh619->gpio_chip.can_sleep   = 1;
543
544         ricoh619->gpio_chip.direction_input     = ricoh619_gpio_input;
545         ricoh619->gpio_chip.direction_output    = ricoh619_gpio_output;
546         ricoh619->gpio_chip.set                 = ricoh619_gpio_set;
547         ricoh619->gpio_chip.get                 = ricoh619_gpio_get;
548         ricoh619->gpio_chip.to_irq              = ricoh619_gpio_to_irq;
549
550         ret = gpiochip_add(&ricoh619->gpio_chip);
551         if (ret)
552                 dev_warn(ricoh619->dev, "GPIO registration failed: %d\n", ret);
553 }
554
555 static int ricoh619_remove_subdev(struct device *dev, void *unused)
556 {
557         platform_device_unregister(to_platform_device(dev));
558         return 0;
559 }
560
561 static int ricoh619_remove_subdevs(struct ricoh619 *ricoh619)
562 {
563         return device_for_each_child(ricoh619->dev, NULL,
564                                      ricoh619_remove_subdev);
565 }
566
567 static int __devinit ricoh619_add_subdevs(struct ricoh619 *ricoh619,
568                                 struct ricoh619_platform_data *pdata)
569 {
570         struct ricoh619_subdev_info *subdev;
571         struct platform_device *pdev;
572         int i, ret = 0;
573
574         for (i = 0; i < pdata->num_subdevs; i++) {
575                 subdev = &pdata->subdevs[i];
576
577                 pdev = platform_device_alloc(subdev->name, subdev->id);
578
579                 pdev->dev.parent = ricoh619->dev;
580                 pdev->dev.platform_data = subdev->platform_data;
581
582                 ret = platform_device_add(pdev);
583                 if (ret)
584                         goto failed;
585         }
586         return 0;
587
588 failed:
589         ricoh619_remove_subdevs(ricoh619);
590         return ret;
591 }
592
593 #ifdef CONFIG_DEBUG_FS
594 #include <linux/debugfs.h>
595 #include <linux/seq_file.h>
596 static void print_regs(const char *header, struct seq_file *s,
597                 struct i2c_client *client, int start_offset,
598                 int end_offset)
599 {
600         uint8_t reg_val;
601         int i;
602         int ret;
603
604         seq_printf(s, "%s\n", header);
605         for (i = start_offset; i <= end_offset; ++i) {
606                 ret = __ricoh619_read(client, i, &reg_val);
607                 if (ret >= 0)
608                         seq_printf(s, "Reg 0x%02x Value 0x%02x\n", i, reg_val);
609         }
610         seq_printf(s, "------------------\n");
611 }
612
613 static int dbg_ricoh_show(struct seq_file *s, void *unused)
614 {
615         struct ricoh619 *ricoh = s->private;
616         struct i2c_client *client = ricoh->client;
617
618         seq_printf(s, "RICOH619 Registers\n");
619         seq_printf(s, "------------------\n");
620
621         print_regs("System Regs",               s, client, 0x0, 0x05);
622         print_regs("Power Control Regs",        s, client, 0x07, 0x2B);
623         print_regs("DCDC  Regs",                s, client, 0x2C, 0x43);
624         print_regs("LDO   Regs",                s, client, 0x44, 0x61);
625         print_regs("ADC   Regs",                s, client, 0x64, 0x8F);
626         print_regs("GPIO  Regs",                s, client, 0x90, 0x98);
627         print_regs("INTC  Regs",                s, client, 0x9C, 0x9E);
628         print_regs("RTC   Regs",                s, client, 0xA0, 0xAF);
629         print_regs("OPT   Regs",                s, client, 0xB0, 0xB1);
630         print_regs("CHG   Regs",                s, client, 0xB2, 0xDF);
631         print_regs("FUEL  Regs",                s, client, 0xE0, 0xFC);
632         return 0;
633 }
634
635 static int dbg_ricoh_open(struct inode *inode, struct file *file)
636 {
637         return single_open(file, dbg_ricoh_show, inode->i_private);
638 }
639
640 static const struct file_operations debug_fops = {
641         .open           = dbg_ricoh_open,
642         .read           = seq_read,
643         .llseek         = seq_lseek,
644         .release        = single_release,
645 };
646 static void __init ricoh619_debuginit(struct ricoh619 *ricoh)
647 {
648         (void)debugfs_create_file("ricoh619", S_IRUGO, NULL,
649                         ricoh, &debug_fops);
650 }
651 #else
652 static void print_regs(const char *header, struct i2c_client *client,
653                 int start_offset, int end_offset)
654 {
655         uint8_t reg_val;
656         int i;
657         int ret;
658
659         printk(KERN_INFO "%s\n", header);
660         for (i = start_offset; i <= end_offset; ++i) {
661                 ret = __ricoh619_read(client, i, &reg_val);
662                 if (ret >= 0)
663                         printk(KERN_INFO "Reg 0x%02x Value 0x%02x\n",
664                                                          i, reg_val);
665         }
666         printk(KERN_INFO "------------------\n");
667 }
668 static void __init ricoh619_debuginit(struct ricoh619 *ricoh)
669 {
670         struct i2c_client *client = ricoh->client;
671
672         printk(KERN_INFO "RICOH619 Registers\n");
673         printk(KERN_INFO "------------------\n");
674
675         print_regs("System Regs",               client, 0x0, 0x05);
676         print_regs("Power Control Regs",        client, 0x07, 0x2B);
677         print_regs("DCDC  Regs",                client, 0x2C, 0x43);
678         print_regs("LDO   Regs",                client, 0x44, 0x5C);
679         print_regs("ADC   Regs",                client, 0x64, 0x8F);
680         print_regs("GPIO  Regs",                client, 0x90, 0x9B);
681         print_regs("INTC  Regs",                client, 0x9C, 0x9E);
682         print_regs("OPT   Regs",                client, 0xB0, 0xB1);
683         print_regs("CHG   Regs",                client, 0xB2, 0xDF);
684         print_regs("FUEL  Regs",                client, 0xE0, 0xFC);
685
686         return 0;
687 }
688 #endif
689
690 static void __devinit ricoh619_noe_init(struct ricoh619 *ricoh)
691 {
692         struct i2c_client *client = ricoh->client;
693         
694         __ricoh619_write(client, RICOH619_PWR_NOE_TIMSET, 0x0); //N_OE timer setting to 128mS
695         __ricoh619_write(client, RICOH619_PWR_REP_CNT, 0x1); //Repeat power ON after reset (Power Off/N_OE)
696 }
697
698 static int ricoh619_i2c_probe(struct i2c_client *client,
699                               const struct i2c_device_id *id)
700 {
701         struct ricoh619 *ricoh619;
702         struct ricoh619_platform_data *pdata = client->dev.platform_data;
703         int ret;
704         uint8_t control;
705         printk(KERN_INFO "PMU: %s:\n", __func__);
706
707         ricoh619 = kzalloc(sizeof(struct ricoh619), GFP_KERNEL);
708         if (ricoh619 == NULL)
709                 return -ENOMEM;
710
711         ricoh619->client = client;
712         ricoh619->dev = &client->dev;
713         i2c_set_clientdata(client, ricoh619);
714
715         mutex_init(&ricoh619->io_lock);
716
717         ret = ricoh619_read(ricoh619->dev, 0x36, &control);
718         if ((control < 0) || (control == 0xff) || (control == 0)) {
719                 printk(KERN_INFO "The device is not ricoh619\n");
720                 return 0;
721         }
722         /***************set noe time 128ms**************/
723         ret = ricoh619_set_bits(ricoh619->dev,0x11,(0x1 <<3));
724         ret = ricoh619_clr_bits(ricoh619->dev,0x11,(0x7 <<0));
725         ret = ricoh619_clr_bits(ricoh619->dev,0x11,(0x1 <<3));
726         /**********************************************/
727
728         /***************set PKEY long press time 0sec*******/
729         ret = ricoh619_set_bits(ricoh619->dev,0x10,(0x1 <<7));
730         ret = ricoh619_clr_bits(ricoh619->dev,0x10,(0x1 <<3));
731         ret = ricoh619_clr_bits(ricoh619->dev,0x10,(0x1 <<7));
732         /**********************************************/
733
734         ricoh619->bank_num = 0;
735
736 //      ret = pdata->init_port(client->irq); // For init PMIC_IRQ port
737         if (client->irq) {
738                 ret = ricoh619_irq_init(ricoh619, client->irq, pdata->irq_base);
739                 if (ret) {
740                         dev_err(&client->dev, "IRQ init failed: %d\n", ret);
741                         goto err_irq_init;
742                 }
743         }
744         
745         ret = ricoh619_add_subdevs(ricoh619, pdata);
746         if (ret) {
747                 dev_err(&client->dev, "add devices failed: %d\n", ret);
748                 goto err_add_devs;
749         }
750         ricoh619_noe_init(ricoh619);
751         
752         g_ricoh619 = ricoh619;
753         if (pdata && pdata->pre_init) {
754                 ret = pdata->pre_init(ricoh619);
755                 if (ret != 0) {
756                         dev_err(ricoh619->dev, "pre_init() failed: %d\n", ret);
757                         goto err;
758                 }
759         }
760
761         //ricoh619_gpio_init(ricoh619, pdata);
762
763         ricoh619_debuginit(ricoh619);
764
765         if (pdata && pdata->post_init) {
766                 ret = pdata->post_init(ricoh619);
767                 if (ret != 0) {
768                         dev_err(ricoh619->dev, "post_init() failed: %d\n", ret);
769                         goto err;
770                 }
771         }
772
773         ricoh619_i2c_client = client;
774         return 0;
775 err:
776         mfd_remove_devices(ricoh619->dev);
777         kfree(ricoh619);
778 err_add_devs:
779         if (client->irq)
780                 ricoh619_irq_exit(ricoh619);
781 err_irq_init:
782         kfree(ricoh619);
783         return ret;
784 }
785
786 static int  __devexit ricoh619_i2c_remove(struct i2c_client *client)
787 {
788         struct ricoh619 *ricoh619 = i2c_get_clientdata(client);
789
790         if (client->irq)
791                 ricoh619_irq_exit(ricoh619);
792
793         ricoh619_remove_subdevs(ricoh619);
794         kfree(ricoh619);
795         return 0;
796 }
797
798 #ifdef CONFIG_PM
799 static int ricoh619_i2c_suspend(struct i2c_client *client, pm_message_t state)
800 {
801         if (client->irq)
802                 disable_irq(client->irq);
803         return 0;
804 }
805
806 int pwrkey_wakeup;
807 static int ricoh619_i2c_resume(struct i2c_client *client)
808 {
809         uint8_t reg_val;
810         int ret;
811
812         ret = __ricoh619_read(client, RICOH619_INT_IR_SYS, &reg_val);
813         if(reg_val & 0x01) { //If PWR_KEY wakeup
814 //              printk("PMU: %s: PWR_KEY Wakeup\n",__func__);
815                 pwrkey_wakeup = 1;
816                 __ricoh619_write(client, RICOH619_INT_IR_SYS, 0x0); //Clear PWR_KEY IRQ
817         }
818
819         enable_irq(client->irq);
820         return 0;
821 }
822
823 #endif
824
825 static const struct i2c_device_id ricoh619_i2c_id[] = {
826         {"ricoh619", 0},
827         {}
828 };
829
830 MODULE_DEVICE_TABLE(i2c, ricoh619_i2c_id);
831
832 static struct i2c_driver ricoh619_i2c_driver = {
833         .driver = {
834                    .name = "ricoh619",
835                    .owner = THIS_MODULE,
836                    },
837         .probe = ricoh619_i2c_probe,
838         .remove = __devexit_p(ricoh619_i2c_remove),
839 #ifdef CONFIG_PM
840         .suspend = ricoh619_i2c_suspend,
841         .resume = ricoh619_i2c_resume,
842 #endif
843         .id_table = ricoh619_i2c_id,
844 };
845
846
847 static int __init ricoh619_i2c_init(void)
848 {
849         int ret = -ENODEV;
850         ret = i2c_add_driver(&ricoh619_i2c_driver);
851         if (ret != 0)
852                 pr_err("Failed to register I2C driver: %d\n", ret);
853
854         return ret;
855 }
856
857 subsys_initcall_sync(ricoh619_i2c_init);
858
859 static void __exit ricoh619_i2c_exit(void)
860 {
861         i2c_del_driver(&ricoh619_i2c_driver);
862 }
863
864 module_exit(ricoh619_i2c_exit);
865
866 MODULE_DESCRIPTION("RICOH RC5T619 PMU multi-function core driver");
867 MODULE_AUTHOR("zhangqing <zhangqing@rock-chips.com>");
868 MODULE_LICENSE("GPL");