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