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