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