2 * driver/mfd/ricoh619.c
4 * Core driver implementation to access RICOH RC5T619 power management chip.
6 * Copyright (C) 2012-2013 RICOH COMPANY,LTD
9 * Copyright (C) 2011 NVIDIA Corporation
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.
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
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/>.
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>
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>
49 struct ricoh619 *g_ricoh619;
50 struct sleep_control_data {
53 static struct mfd_cell ricoh619s[] = {
55 .name = "ricoh619-regulator",
58 .name = "ricoh619-battery",
61 .name = "ricoh619-rtc",
64 .name = "ricoh619-pwrkey",
69 #define SLEEP_INIT(_id, _reg) \
70 [RICOH619_DS_##_id] = {.reg_add = _reg}
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),
96 static inline int __ricoh619_read(struct i2c_client *client,
100 ret = i2c_smbus_read_byte_data(client, reg);
102 dev_err(&client->dev, "failed reading at 0x%02x %d\n", reg,ret);
107 dev_dbg(&client->dev, "ricoh619: reg read reg=%x, val=%x\n",
112 static inline int __ricoh619_bulk_reads(struct i2c_client *client, u8 reg,
113 int len, uint8_t *val)
118 ret = i2c_smbus_read_i2c_block_data(client, reg, len, val);
120 dev_err(&client->dev, "failed reading from 0x%02x %dn", reg,ret);
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));
130 static inline int __ricoh619_write(struct i2c_client *client,
135 dev_dbg(&client->dev, "ricoh619: reg write reg=%x, val=%x\n",
137 ret = i2c_smbus_write_byte_data(client, reg, val);
139 dev_err(&client->dev, "failed writing 0x%02x to 0x%02x\n",
146 static inline int __ricoh619_bulk_writes(struct i2c_client *client, u8 reg,
147 int len, uint8_t *val)
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));
157 ret = i2c_smbus_write_i2c_block_data(client, reg, len, val);
159 dev_err(&client->dev, "failed writings to 0x%02x\n", reg);
165 static inline int set_bank_ricoh619(struct device *dev, int bank)
167 struct ricoh619 *ricoh619 = dev_get_drvdata(dev);
170 if (bank != (bank & 1))
172 if (bank == ricoh619->bank_num)
174 ret = __ricoh619_write(to_i2c_client(dev), RICOH619_REG_BANKSEL, bank);
176 ricoh619->bank_num = bank;
181 int ricoh619_write(struct device *dev, u8 reg, uint8_t val)
183 struct ricoh619 *ricoh619 = dev_get_drvdata(dev);
186 mutex_lock(&ricoh619->io_lock);
187 ret = set_bank_ricoh619(dev, 0);
189 ret = __ricoh619_write(to_i2c_client(dev), reg, val);
190 mutex_unlock(&ricoh619->io_lock);
194 EXPORT_SYMBOL_GPL(ricoh619_write);
196 int ricoh619_write_bank1(struct device *dev, u8 reg, uint8_t val)
198 struct ricoh619 *ricoh619 = dev_get_drvdata(dev);
201 mutex_lock(&ricoh619->io_lock);
202 ret = set_bank_ricoh619(dev, 1);
204 ret = __ricoh619_write(to_i2c_client(dev), reg, val);
205 mutex_unlock(&ricoh619->io_lock);
209 EXPORT_SYMBOL_GPL(ricoh619_write_bank1);
211 int ricoh619_bulk_writes(struct device *dev, u8 reg, u8 len, uint8_t *val)
213 struct ricoh619 *ricoh619 = dev_get_drvdata(dev);
216 mutex_lock(&ricoh619->io_lock);
217 ret = set_bank_ricoh619(dev, 0);
219 ret = __ricoh619_bulk_writes(to_i2c_client(dev), reg, len, val);
220 mutex_unlock(&ricoh619->io_lock);
224 EXPORT_SYMBOL_GPL(ricoh619_bulk_writes);
226 int ricoh619_bulk_writes_bank1(struct device *dev, u8 reg, u8 len, uint8_t *val)
228 struct ricoh619 *ricoh619 = dev_get_drvdata(dev);
231 mutex_lock(&ricoh619->io_lock);
232 ret = set_bank_ricoh619(dev, 1);
234 ret = __ricoh619_bulk_writes(to_i2c_client(dev), reg, len, val);
235 mutex_unlock(&ricoh619->io_lock);
239 EXPORT_SYMBOL_GPL(ricoh619_bulk_writes_bank1);
241 int ricoh619_read(struct device *dev, u8 reg, uint8_t *val)
243 struct ricoh619 *ricoh619 = dev_get_drvdata(dev);
246 mutex_lock(&ricoh619->io_lock);
247 ret = set_bank_ricoh619(dev, 0);
249 ret = __ricoh619_read(to_i2c_client(dev), reg, val);
250 mutex_unlock(&ricoh619->io_lock);
254 EXPORT_SYMBOL_GPL(ricoh619_read);
256 int ricoh619_read_bank1(struct device *dev, u8 reg, uint8_t *val)
258 struct ricoh619 *ricoh619 = dev_get_drvdata(dev);
261 mutex_lock(&ricoh619->io_lock);
262 ret = set_bank_ricoh619(dev, 1);
264 ret = __ricoh619_read(to_i2c_client(dev), reg, val);
265 mutex_unlock(&ricoh619->io_lock);
270 EXPORT_SYMBOL_GPL(ricoh619_read_bank1);
272 int ricoh619_bulk_reads(struct device *dev, u8 reg, u8 len, uint8_t *val)
274 struct ricoh619 *ricoh619 = dev_get_drvdata(dev);
277 mutex_lock(&ricoh619->io_lock);
278 ret = set_bank_ricoh619(dev, 0);
280 ret = __ricoh619_bulk_reads(to_i2c_client(dev), reg, len, val);
281 mutex_unlock(&ricoh619->io_lock);
285 EXPORT_SYMBOL_GPL(ricoh619_bulk_reads);
287 int ricoh619_bulk_reads_bank1(struct device *dev, u8 reg, u8 len, uint8_t *val)
289 struct ricoh619 *ricoh619 = dev_get_drvdata(dev);
292 mutex_lock(&ricoh619->io_lock);
293 ret = set_bank_ricoh619(dev, 1);
295 ret = __ricoh619_bulk_reads(to_i2c_client(dev), reg, len, val);
296 mutex_unlock(&ricoh619->io_lock);
300 EXPORT_SYMBOL_GPL(ricoh619_bulk_reads_bank1);
302 int ricoh619_set_bits(struct device *dev, u8 reg, uint8_t bit_mask)
304 struct ricoh619 *ricoh619 = dev_get_drvdata(dev);
308 mutex_lock(&ricoh619->io_lock);
309 ret = set_bank_ricoh619(dev, 0);
311 ret = __ricoh619_read(to_i2c_client(dev), reg, ®_val);
315 if ((reg_val & bit_mask) != bit_mask) {
317 ret = __ricoh619_write(to_i2c_client(dev), reg,
322 mutex_unlock(&ricoh619->io_lock);
325 EXPORT_SYMBOL_GPL(ricoh619_set_bits);
327 int ricoh619_clr_bits(struct device *dev, u8 reg, uint8_t bit_mask)
329 struct ricoh619 *ricoh619 = dev_get_drvdata(dev);
333 mutex_lock(&ricoh619->io_lock);
334 ret = set_bank_ricoh619(dev, 0);
336 ret = __ricoh619_read(to_i2c_client(dev), reg, ®_val);
340 if (reg_val & bit_mask) {
341 reg_val &= ~bit_mask;
342 ret = __ricoh619_write(to_i2c_client(dev), reg,
347 mutex_unlock(&ricoh619->io_lock);
350 EXPORT_SYMBOL_GPL(ricoh619_clr_bits);
352 int ricoh619_update(struct device *dev, u8 reg, uint8_t val, uint8_t mask)
354 struct ricoh619 *ricoh619 = dev_get_drvdata(dev);
358 mutex_lock(&ricoh619->io_lock);
359 ret = set_bank_ricoh619(dev, 0);
361 ret = __ricoh619_read(ricoh619->client, reg, ®_val);
365 if ((reg_val & mask) != val) {
366 reg_val = (reg_val & ~mask) | (val & mask);
367 ret = __ricoh619_write(ricoh619->client, reg, reg_val);
371 mutex_unlock(&ricoh619->io_lock);
374 EXPORT_SYMBOL_GPL(ricoh619_update);
376 int ricoh619_update_bank1(struct device *dev, u8 reg, uint8_t val, uint8_t mask)
378 struct ricoh619 *ricoh619 = dev_get_drvdata(dev);
382 mutex_lock(&ricoh619->io_lock);
383 ret = set_bank_ricoh619(dev, 1);
385 ret = __ricoh619_read(ricoh619->client, reg, ®_val);
389 if ((reg_val & mask) != val) {
390 reg_val = (reg_val & ~mask) | (val & mask);
391 ret = __ricoh619_write(ricoh619->client, reg, reg_val);
395 mutex_unlock(&ricoh619->io_lock);
399 static struct i2c_client *ricoh619_i2c_client;
400 static void ricoh619_device_shutdown(void)
404 struct ricoh619 *ricoh619 = g_ricoh619;
405 printk("%s,line=%d\n", __func__,__LINE__);
407 #ifdef CONFIG_BATTERY_RICOH619
410 ret = ricoh619_write(ricoh619->dev, RICOH619_PSWR, val);
412 dev_err(ricoh619->dev, "Error in writing PSWR_REG\n");
414 if (g_fg_on_mode == 0) {
415 ret = ricoh619_clr_bits(ricoh619->dev,
416 RICOH619_FG_CTRL, 0x01);
418 dev_err(ricoh619->dev, "Error in writing FG_CTRL\n");
421 /* set rapid timer 300 min */
422 ret = ricoh619_set_bits(ricoh619->dev, TIMSET_REG, 0x03);
424 dev_err(ricoh619->dev, "Error in writing the TIMSET_Reg\n");
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);
432 EXPORT_SYMBOL_GPL(ricoh619_device_shutdown);
433 static void ricoh619_power_off(void)
436 uint8_t val,charge_state;
437 struct i2c_client *client = ricoh619_i2c_client;
440 printk("%s,line=%d\n", __func__,__LINE__);
441 #ifdef CONFIG_BATTERY_RICOH619
442 ret = __ricoh619_read(client, 0xBD, &val);
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
450 ret = __ricoh619_write(client, RICOH619_PWR_REP_CNT,(val |(0x1<<0)));//Power OFF
455 ret = __ricoh619_read(client, RICOH619_PWR_SLP_CNT,&val);//Power OFF
458 ret = __ricoh619_write(client, RICOH619_PWR_SLP_CNT,(val |(0x1<<0)));//Power OFF
460 printk("ricoh619 power off error!\n");
466 EXPORT_SYMBOL_GPL(ricoh619_power_off);
469 static int ricoh619_gpio_get(struct gpio_chip *gc, unsigned offset)
471 struct ricoh619 *ricoh619 = container_of(gc, struct ricoh619, gpio_chip);
475 ret = ricoh619_read(ricoh619->dev, RICOH619_GPIO_MON_IOIN, &val);
479 return ((val & (0x1 << offset)) != 0);
482 static void ricoh619_gpio_set(struct gpio_chip *gc, unsigned offset,
485 struct ricoh619 *ricoh619 = container_of(gc, struct ricoh619, gpio_chip);
487 ricoh619_set_bits(ricoh619->dev, RICOH619_GPIO_IOOUT,
490 ricoh619_clr_bits(ricoh619->dev, RICOH619_GPIO_IOOUT,
494 static int ricoh619_gpio_input(struct gpio_chip *gc, unsigned offset)
496 struct ricoh619 *ricoh619 = container_of(gc, struct ricoh619, gpio_chip);
498 return ricoh619_clr_bits(ricoh619->dev, RICOH619_GPIO_IOSEL,
502 static int ricoh619_gpio_output(struct gpio_chip *gc, unsigned offset,
505 struct ricoh619 *ricoh619 = container_of(gc, struct ricoh619, gpio_chip);
507 ricoh619_gpio_set(gc, offset, value);
508 return ricoh619_set_bits(ricoh619->dev, RICOH619_GPIO_IOSEL,
512 static int ricoh619_gpio_to_irq(struct gpio_chip *gc, unsigned off)
514 struct ricoh619 *ricoh619 = container_of(gc, struct ricoh619, gpio_chip);
516 if ((off >= 0) && (off < 8))
517 return ricoh619->irq_base + RICOH619_IRQ_GPIO0 + off;
522 static void ricoh619_gpio_init(struct ricoh619 *ricoh619,
523 struct ricoh619_platform_data *pdata)
527 struct ricoh619_gpio_init_data *ginit;
529 if (pdata->gpio_base <= 0)
532 for (i = 0; i < pdata->num_gpioinit_data; ++i) {
533 ginit = &pdata->gpio_init_data[i];
535 if (!ginit->init_apply)
538 if (ginit->output_mode_en) {
539 /* GPIO output mode */
540 if (ginit->output_val)
542 ret = ricoh619_set_bits(ricoh619->dev,
543 RICOH619_GPIO_IOOUT, 1 << i);
546 ret = ricoh619_clr_bits(ricoh619->dev,
547 RICOH619_GPIO_IOOUT, 1 << i);
549 ret = ricoh619_set_bits(ricoh619->dev,
550 RICOH619_GPIO_IOSEL, 1 << i);
552 /* GPIO input mode */
553 ret = ricoh619_clr_bits(ricoh619->dev,
554 RICOH619_GPIO_IOSEL, 1 << i);
556 /* if LED function enabled in OTP */
557 if (ginit->led_mode) {
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);
572 dev_err(ricoh619->dev, "Gpio %d init "
573 "dir configuration failed: %d\n", i, ret);
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;
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;
590 ret = gpiochip_add(&ricoh619->gpio_chip);
592 dev_warn(ricoh619->dev, "GPIO registration failed: %d\n", ret);
595 static int ricoh619_remove_subdev(struct device *dev, void *unused)
597 platform_device_unregister(to_platform_device(dev));
601 static int ricoh619_remove_subdevs(struct ricoh619 *ricoh619)
603 return device_for_each_child(ricoh619->dev, NULL,
604 ricoh619_remove_subdev);
607 static int ricoh619_add_subdevs(struct ricoh619 *ricoh619,
608 struct ricoh619_platform_data *pdata)
610 struct ricoh619_subdev_info *subdev;
611 struct platform_device *pdev;
614 for (i = 0; i < pdata->num_subdevs; i++) {
615 subdev = &pdata->subdevs[i];
617 pdev = platform_device_alloc(subdev->name, subdev->id);
619 pdev->dev.parent = ricoh619->dev;
620 pdev->dev.platform_data = subdev->platform_data;
622 ret = platform_device_add(pdev);
629 ricoh619_remove_subdevs(ricoh619);
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,
644 seq_printf(s, "%s\n", header);
645 for (i = start_offset; i <= end_offset; ++i) {
646 ret = __ricoh619_read(client, i, ®_val);
648 seq_printf(s, "Reg 0x%02x Value 0x%02x\n", i, reg_val);
650 seq_printf(s, "------------------\n");
653 static int dbg_ricoh_show(struct seq_file *s, void *unused)
655 struct ricoh619 *ricoh = s->private;
656 struct i2c_client *client = ricoh->client;
658 seq_printf(s, "RICOH619 Registers\n");
659 seq_printf(s, "------------------\n");
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);
675 static int dbg_ricoh_open(struct inode *inode, struct file *file)
677 return single_open(file, dbg_ricoh_show, inode->i_private);
680 static const struct file_operations debug_fops = {
681 .open = dbg_ricoh_open,
684 .release = single_release,
686 static void __init ricoh619_debuginit(struct ricoh619 *ricoh)
688 (void)debugfs_create_file("ricoh619", S_IRUGO, NULL,
692 static void print_regs(const char *header, struct i2c_client *client,
693 int start_offset, int end_offset)
699 printk(KERN_INFO "%s\n", header);
700 for (i = start_offset; i <= end_offset; ++i) {
701 ret = __ricoh619_read(client, i, ®_val);
703 printk(KERN_INFO "Reg 0x%02x Value 0x%02x\n",
706 printk(KERN_INFO "------------------\n");
708 static void __init ricoh619_debuginit(struct ricoh619 *ricoh)
710 struct i2c_client *client = ricoh->client;
712 printk(KERN_INFO "RICOH619 Registers\n");
713 printk(KERN_INFO "------------------\n");
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);
731 static struct ricoh619_platform_data *ricoh619_parse_dt(struct ricoh619 *ricoh619)
733 struct ricoh619_platform_data *pdata;
734 struct device_node *ricoh619_pmic_np;
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");
741 pdata = devm_kzalloc(ricoh619->dev, sizeof(*pdata), GFP_KERNEL);
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);
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);
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);
760 pdata->pmic_sleep = true;
762 pdata->pm_off = of_property_read_bool(ricoh619_pmic_np,"ricoh619,system-power-controller");
768 static struct ricoh619_platform_data *ricoh619_parse_dt(struct ricoh619 *ricoh619)
774 static struct syscore_ops ricoh619_syscore_ops = {
775 .shutdown = ricoh619_device_shutdown,
778 static void ricoh619_noe_init(struct ricoh619 *ricoh)
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
790 static int ricoh619_pre_init(struct ricoh619 *ricoh619)
793 printk("%s,line=%d\n", __func__,__LINE__);
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);
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
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
817 static int ricoh619_i2c_probe(struct i2c_client *client,
818 const struct i2c_device_id *id)
820 struct ricoh619 *ricoh619;
821 struct ricoh619_platform_data *pdata;
825 printk("%s,line=%d\n", __func__,__LINE__);
827 ricoh619 = devm_kzalloc(&client->dev,sizeof(struct ricoh619), GFP_KERNEL);
828 if (ricoh619 == NULL)
831 ricoh619->client = client;
832 ricoh619->dev = &client->dev;
833 i2c_set_clientdata(client, ricoh619);
834 mutex_init(&ricoh619->io_lock);
836 ret = ricoh619_read(ricoh619->dev, 0x36, &control);
837 if ((ret <0) || (control < 0) || (control == 0xff) || (control == 0) ){
839 printk(KERN_INFO "The device is not ricoh619 %08x %d\n",control,ret);
844 ret = ricoh619_write(ricoh619->dev, 0xff, 0x00);
845 ret = ricoh619_read(ricoh619->dev, 0x36, &control);
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) ){
851 printk(KERN_INFO "##################The device is not ricoh619 %08x %d\n",control,ret);
857 ret = ricoh619_pre_init(ricoh619);
859 printk("The ricoh619_pre_init failed %d\n",ret);
863 ricoh619->bank_num = 0;
865 if (ricoh619->dev->of_node)
866 pdata = ricoh619_parse_dt(ricoh619);
869 ricoh619->dc_det = pdata->dc_det;
871 /******************************set sleep vol & dcdc mode******************/
873 if (pdata->pmic_sleep_gpio) {
874 ret = gpio_request(pdata->pmic_sleep_gpio, "ricoh619_pmic_sleep");
876 dev_err(ricoh619->dev,"Failed to request gpio %d with ret:""%d\n", pdata->pmic_sleep_gpio, ret);
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);
885 /**********************************************************/
886 ret = ricoh619_irq_init(ricoh619, pdata->irq_gpio, pdata);
890 ret = mfd_add_devices(ricoh619->dev, -1,
891 ricoh619s, ARRAY_SIZE(ricoh619s),
893 g_ricoh619 = ricoh619;
894 if (pdata->pm_off && !pm_power_off) {
895 pm_power_off = ricoh619_power_off;
897 ricoh619_debuginit(ricoh619);
899 ricoh619_i2c_client = client;
900 register_syscore_ops(&ricoh619_syscore_ops);
903 mfd_remove_devices(ricoh619->dev);
907 static int ricoh619_i2c_remove(struct i2c_client *client)
909 struct ricoh619 *ricoh619 = i2c_get_clientdata(client);
911 unregister_syscore_ops(&ricoh619_syscore_ops);
912 ricoh619_remove_subdevs(ricoh619);
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)
921 // printk("PMU: %s: \n",__func__);
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);
930 static int ricoh619_i2c_resume(struct i2c_client *client)
935 ret = __ricoh619_read(client, RICOH619_INT_IR_SYS, ®_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
943 if (g_ricoh619->chip_irq)
944 enable_irq(g_ricoh619->chip_irq);
948 static int ricoh619_i2c_late_suspend(struct device *dev)
950 struct i2c_client *client = i2c_verify_client(dev);
952 ricoh619_i2c_suspend(client,PMSG_SUSPEND);
956 static int rockchip_i2c_late_resume(struct device *dev)
958 struct i2c_client *client = i2c_verify_client(dev);
960 ricoh619_i2c_resume(client);
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,
971 static const struct i2c_device_id ricoh619_i2c_id[] = {
975 MODULE_DEVICE_TABLE(i2c, ricoh619_i2c_id);
978 static const struct of_device_id ricoh619_dt_match[] = {
979 { .compatible = "ricoh,ricoh619", },
982 MODULE_DEVICE_TABLE(of, ricoh619_dt_match);
985 static struct i2c_driver ricoh619_i2c_driver = {
988 .owner = THIS_MODULE,
990 .pm = (&ricoh619_i2c_dev_pm),
992 .of_match_table = of_match_ptr(ricoh619_dt_match),
994 .probe = ricoh619_i2c_probe,
995 .remove = ricoh619_i2c_remove,
997 .id_table = ricoh619_i2c_id,
1001 static int __init ricoh619_i2c_init(void)
1004 ret = i2c_add_driver(&ricoh619_i2c_driver);
1006 pr_err("Failed to register I2C driver: %d\n", ret);
1011 subsys_initcall_sync(ricoh619_i2c_init);
1013 static void __exit ricoh619_i2c_exit(void)
1015 i2c_del_driver(&ricoh619_i2c_driver);
1018 module_exit(ricoh619_i2c_exit);
1020 MODULE_DESCRIPTION("RICOH RC5T619 PMU multi-function core driver");
1021 MODULE_AUTHOR("zhangqing <zhangqing@rock-chips.com>");
1022 MODULE_LICENSE("GPL");