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>
48 struct ricoh619 *g_ricoh619;
49 struct sleep_control_data {
52 static struct mfd_cell ricoh619s[] = {
54 .name = "ricoh619-regulator",
57 .name = "ricoh619-battery",
60 .name = "ricoh619-rtc",
63 .name = "ricoh619-pwrkey",
68 #define SLEEP_INIT(_id, _reg) \
69 [RICOH619_DS_##_id] = {.reg_add = _reg}
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),
95 static inline int __ricoh619_read(struct i2c_client *client,
99 ret = i2c_smbus_read_byte_data(client, reg);
101 dev_err(&client->dev, "failed reading at 0x%02x %d\n", reg,ret);
106 dev_dbg(&client->dev, "ricoh619: reg read reg=%x, val=%x\n",
111 static inline int __ricoh619_bulk_reads(struct i2c_client *client, u8 reg,
112 int len, uint8_t *val)
117 ret = i2c_smbus_read_i2c_block_data(client, reg, len, val);
119 dev_err(&client->dev, "failed reading from 0x%02x %dn", reg,ret);
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));
129 static inline int __ricoh619_write(struct i2c_client *client,
134 dev_dbg(&client->dev, "ricoh619: reg write reg=%x, val=%x\n",
136 ret = i2c_smbus_write_byte_data(client, reg, val);
138 dev_err(&client->dev, "failed writing 0x%02x to 0x%02x\n",
145 static inline int __ricoh619_bulk_writes(struct i2c_client *client, u8 reg,
146 int len, uint8_t *val)
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));
156 ret = i2c_smbus_write_i2c_block_data(client, reg, len, val);
158 dev_err(&client->dev, "failed writings to 0x%02x\n", reg);
164 static inline int set_bank_ricoh619(struct device *dev, int bank)
166 struct ricoh619 *ricoh619 = dev_get_drvdata(dev);
169 if (bank != (bank & 1))
171 if (bank == ricoh619->bank_num)
173 ret = __ricoh619_write(to_i2c_client(dev), RICOH619_REG_BANKSEL, bank);
175 ricoh619->bank_num = bank;
180 int ricoh619_write(struct device *dev, u8 reg, uint8_t val)
182 struct ricoh619 *ricoh619 = dev_get_drvdata(dev);
185 mutex_lock(&ricoh619->io_lock);
186 ret = set_bank_ricoh619(dev, 0);
188 ret = __ricoh619_write(to_i2c_client(dev), reg, val);
189 mutex_unlock(&ricoh619->io_lock);
193 EXPORT_SYMBOL_GPL(ricoh619_write);
195 int ricoh619_write_bank1(struct device *dev, u8 reg, uint8_t val)
197 struct ricoh619 *ricoh619 = dev_get_drvdata(dev);
200 mutex_lock(&ricoh619->io_lock);
201 ret = set_bank_ricoh619(dev, 1);
203 ret = __ricoh619_write(to_i2c_client(dev), reg, val);
204 mutex_unlock(&ricoh619->io_lock);
208 EXPORT_SYMBOL_GPL(ricoh619_write_bank1);
210 int ricoh619_bulk_writes(struct device *dev, u8 reg, u8 len, uint8_t *val)
212 struct ricoh619 *ricoh619 = dev_get_drvdata(dev);
215 mutex_lock(&ricoh619->io_lock);
216 ret = set_bank_ricoh619(dev, 0);
218 ret = __ricoh619_bulk_writes(to_i2c_client(dev), reg, len, val);
219 mutex_unlock(&ricoh619->io_lock);
223 EXPORT_SYMBOL_GPL(ricoh619_bulk_writes);
225 int ricoh619_bulk_writes_bank1(struct device *dev, u8 reg, u8 len, uint8_t *val)
227 struct ricoh619 *ricoh619 = dev_get_drvdata(dev);
230 mutex_lock(&ricoh619->io_lock);
231 ret = set_bank_ricoh619(dev, 1);
233 ret = __ricoh619_bulk_writes(to_i2c_client(dev), reg, len, val);
234 mutex_unlock(&ricoh619->io_lock);
238 EXPORT_SYMBOL_GPL(ricoh619_bulk_writes_bank1);
240 int ricoh619_read(struct device *dev, u8 reg, uint8_t *val)
242 struct ricoh619 *ricoh619 = dev_get_drvdata(dev);
245 mutex_lock(&ricoh619->io_lock);
246 ret = set_bank_ricoh619(dev, 0);
248 ret = __ricoh619_read(to_i2c_client(dev), reg, val);
249 mutex_unlock(&ricoh619->io_lock);
253 EXPORT_SYMBOL_GPL(ricoh619_read);
255 int ricoh619_read_bank1(struct device *dev, u8 reg, uint8_t *val)
257 struct ricoh619 *ricoh619 = dev_get_drvdata(dev);
260 mutex_lock(&ricoh619->io_lock);
261 ret = set_bank_ricoh619(dev, 1);
263 ret = __ricoh619_read(to_i2c_client(dev), reg, val);
264 mutex_unlock(&ricoh619->io_lock);
269 EXPORT_SYMBOL_GPL(ricoh619_read_bank1);
271 int ricoh619_bulk_reads(struct device *dev, u8 reg, u8 len, uint8_t *val)
273 struct ricoh619 *ricoh619 = dev_get_drvdata(dev);
276 mutex_lock(&ricoh619->io_lock);
277 ret = set_bank_ricoh619(dev, 0);
279 ret = __ricoh619_bulk_reads(to_i2c_client(dev), reg, len, val);
280 mutex_unlock(&ricoh619->io_lock);
284 EXPORT_SYMBOL_GPL(ricoh619_bulk_reads);
286 int ricoh619_bulk_reads_bank1(struct device *dev, u8 reg, u8 len, uint8_t *val)
288 struct ricoh619 *ricoh619 = dev_get_drvdata(dev);
291 mutex_lock(&ricoh619->io_lock);
292 ret = set_bank_ricoh619(dev, 1);
294 ret = __ricoh619_bulk_reads(to_i2c_client(dev), reg, len, val);
295 mutex_unlock(&ricoh619->io_lock);
299 EXPORT_SYMBOL_GPL(ricoh619_bulk_reads_bank1);
301 int ricoh619_set_bits(struct device *dev, u8 reg, uint8_t bit_mask)
303 struct ricoh619 *ricoh619 = dev_get_drvdata(dev);
307 mutex_lock(&ricoh619->io_lock);
308 ret = set_bank_ricoh619(dev, 0);
310 ret = __ricoh619_read(to_i2c_client(dev), reg, ®_val);
314 if ((reg_val & bit_mask) != bit_mask) {
316 ret = __ricoh619_write(to_i2c_client(dev), reg,
321 mutex_unlock(&ricoh619->io_lock);
324 EXPORT_SYMBOL_GPL(ricoh619_set_bits);
326 int ricoh619_clr_bits(struct device *dev, u8 reg, uint8_t bit_mask)
328 struct ricoh619 *ricoh619 = dev_get_drvdata(dev);
332 mutex_lock(&ricoh619->io_lock);
333 ret = set_bank_ricoh619(dev, 0);
335 ret = __ricoh619_read(to_i2c_client(dev), reg, ®_val);
339 if (reg_val & bit_mask) {
340 reg_val &= ~bit_mask;
341 ret = __ricoh619_write(to_i2c_client(dev), reg,
346 mutex_unlock(&ricoh619->io_lock);
349 EXPORT_SYMBOL_GPL(ricoh619_clr_bits);
351 int ricoh619_update(struct device *dev, u8 reg, uint8_t val, uint8_t mask)
353 struct ricoh619 *ricoh619 = dev_get_drvdata(dev);
357 mutex_lock(&ricoh619->io_lock);
358 ret = set_bank_ricoh619(dev, 0);
360 ret = __ricoh619_read(ricoh619->client, reg, ®_val);
364 if ((reg_val & mask) != val) {
365 reg_val = (reg_val & ~mask) | (val & mask);
366 ret = __ricoh619_write(ricoh619->client, reg, reg_val);
370 mutex_unlock(&ricoh619->io_lock);
373 EXPORT_SYMBOL_GPL(ricoh619_update);
375 int ricoh619_update_bank1(struct device *dev, u8 reg, uint8_t val, uint8_t mask)
377 struct ricoh619 *ricoh619 = dev_get_drvdata(dev);
381 mutex_lock(&ricoh619->io_lock);
382 ret = set_bank_ricoh619(dev, 1);
384 ret = __ricoh619_read(ricoh619->client, reg, ®_val);
388 if ((reg_val & mask) != val) {
389 reg_val = (reg_val & ~mask) | (val & mask);
390 ret = __ricoh619_write(ricoh619->client, reg, reg_val);
394 mutex_unlock(&ricoh619->io_lock);
398 static struct i2c_client *ricoh619_i2c_client;
399 static void ricoh619_device_shutdown(struct i2c_client *client)
403 struct ricoh619 *ricoh619 = g_ricoh619;
404 printk("%s,line=%d\n", __func__,__LINE__);
406 #ifdef CONFIG_BATTERY_RICOH619
409 ret = ricoh619_write(ricoh619->dev, RICOH619_PSWR, val);
411 dev_err(ricoh619->dev, "Error in writing PSWR_REG\n");
413 if (g_fg_on_mode == 0) {
414 ret = ricoh619_clr_bits(ricoh619->dev,
415 RICOH619_FG_CTRL, 0x01);
417 dev_err(ricoh619->dev, "Error in writing FG_CTRL\n");
420 /* set rapid timer 300 min */
421 ret = ricoh619_set_bits(ricoh619->dev, TIMSET_REG, 0x03);
423 dev_err(ricoh619->dev, "Error in writing the TIMSET_Reg\n");
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);
431 EXPORT_SYMBOL_GPL(ricoh619_device_shutdown);
432 static void ricoh619_power_off(void)
435 uint8_t val,charge_state;
436 struct i2c_client *client = ricoh619_i2c_client;
439 printk("%s,line=%d\n", __func__,__LINE__);
440 #ifdef CONFIG_BATTERY_RICOH619
441 ret = __ricoh619_read(client, 0xBD, &val);
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
449 ret = __ricoh619_write(client, RICOH619_PWR_REP_CNT,(val |(0x1<<0)));//Power OFF
454 ret = __ricoh619_read(client, RICOH619_PWR_SLP_CNT,&val);//Power OFF
457 ret = __ricoh619_write(client, RICOH619_PWR_SLP_CNT,(val |(0x1<<0)));//Power OFF
459 printk("ricoh619 power off error!\n");
465 EXPORT_SYMBOL_GPL(ricoh619_power_off);
468 static int ricoh619_gpio_get(struct gpio_chip *gc, unsigned offset)
470 struct ricoh619 *ricoh619 = container_of(gc, struct ricoh619, gpio_chip);
474 ret = ricoh619_read(ricoh619->dev, RICOH619_GPIO_MON_IOIN, &val);
478 return ((val & (0x1 << offset)) != 0);
481 static void ricoh619_gpio_set(struct gpio_chip *gc, unsigned offset,
484 struct ricoh619 *ricoh619 = container_of(gc, struct ricoh619, gpio_chip);
486 ricoh619_set_bits(ricoh619->dev, RICOH619_GPIO_IOOUT,
489 ricoh619_clr_bits(ricoh619->dev, RICOH619_GPIO_IOOUT,
493 static int ricoh619_gpio_input(struct gpio_chip *gc, unsigned offset)
495 struct ricoh619 *ricoh619 = container_of(gc, struct ricoh619, gpio_chip);
497 return ricoh619_clr_bits(ricoh619->dev, RICOH619_GPIO_IOSEL,
501 static int ricoh619_gpio_output(struct gpio_chip *gc, unsigned offset,
504 struct ricoh619 *ricoh619 = container_of(gc, struct ricoh619, gpio_chip);
506 ricoh619_gpio_set(gc, offset, value);
507 return ricoh619_set_bits(ricoh619->dev, RICOH619_GPIO_IOSEL,
511 static int ricoh619_gpio_to_irq(struct gpio_chip *gc, unsigned off)
513 struct ricoh619 *ricoh619 = container_of(gc, struct ricoh619, gpio_chip);
515 if ((off >= 0) && (off < 8))
516 return ricoh619->irq_base + RICOH619_IRQ_GPIO0 + off;
521 static void ricoh619_gpio_init(struct ricoh619 *ricoh619,
522 struct ricoh619_platform_data *pdata)
526 struct ricoh619_gpio_init_data *ginit;
528 if (pdata->gpio_base <= 0)
531 for (i = 0; i < pdata->num_gpioinit_data; ++i) {
532 ginit = &pdata->gpio_init_data[i];
534 if (!ginit->init_apply)
537 if (ginit->output_mode_en) {
538 /* GPIO output mode */
539 if (ginit->output_val)
541 ret = ricoh619_set_bits(ricoh619->dev,
542 RICOH619_GPIO_IOOUT, 1 << i);
545 ret = ricoh619_clr_bits(ricoh619->dev,
546 RICOH619_GPIO_IOOUT, 1 << i);
548 ret = ricoh619_set_bits(ricoh619->dev,
549 RICOH619_GPIO_IOSEL, 1 << i);
551 /* GPIO input mode */
552 ret = ricoh619_clr_bits(ricoh619->dev,
553 RICOH619_GPIO_IOSEL, 1 << i);
555 /* if LED function enabled in OTP */
556 if (ginit->led_mode) {
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);
571 dev_err(ricoh619->dev, "Gpio %d init "
572 "dir configuration failed: %d\n", i, ret);
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;
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;
589 ret = gpiochip_add(&ricoh619->gpio_chip);
591 dev_warn(ricoh619->dev, "GPIO registration failed: %d\n", ret);
594 static int ricoh619_remove_subdev(struct device *dev, void *unused)
596 platform_device_unregister(to_platform_device(dev));
600 static int ricoh619_remove_subdevs(struct ricoh619 *ricoh619)
602 return device_for_each_child(ricoh619->dev, NULL,
603 ricoh619_remove_subdev);
606 static int ricoh619_add_subdevs(struct ricoh619 *ricoh619,
607 struct ricoh619_platform_data *pdata)
609 struct ricoh619_subdev_info *subdev;
610 struct platform_device *pdev;
613 for (i = 0; i < pdata->num_subdevs; i++) {
614 subdev = &pdata->subdevs[i];
616 pdev = platform_device_alloc(subdev->name, subdev->id);
618 pdev->dev.parent = ricoh619->dev;
619 pdev->dev.platform_data = subdev->platform_data;
621 ret = platform_device_add(pdev);
628 ricoh619_remove_subdevs(ricoh619);
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,
643 seq_printf(s, "%s\n", header);
644 for (i = start_offset; i <= end_offset; ++i) {
645 ret = __ricoh619_read(client, i, ®_val);
647 seq_printf(s, "Reg 0x%02x Value 0x%02x\n", i, reg_val);
649 seq_printf(s, "------------------\n");
652 static int dbg_ricoh_show(struct seq_file *s, void *unused)
654 struct ricoh619 *ricoh = s->private;
655 struct i2c_client *client = ricoh->client;
657 seq_printf(s, "RICOH619 Registers\n");
658 seq_printf(s, "------------------\n");
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);
674 static int dbg_ricoh_open(struct inode *inode, struct file *file)
676 return single_open(file, dbg_ricoh_show, inode->i_private);
679 static const struct file_operations debug_fops = {
680 .open = dbg_ricoh_open,
683 .release = single_release,
685 static void __init ricoh619_debuginit(struct ricoh619 *ricoh)
687 (void)debugfs_create_file("ricoh619", S_IRUGO, NULL,
691 static void print_regs(const char *header, struct i2c_client *client,
692 int start_offset, int end_offset)
698 printk(KERN_INFO "%s\n", header);
699 for (i = start_offset; i <= end_offset; ++i) {
700 ret = __ricoh619_read(client, i, ®_val);
702 printk(KERN_INFO "Reg 0x%02x Value 0x%02x\n",
705 printk(KERN_INFO "------------------\n");
707 static void __init ricoh619_debuginit(struct ricoh619 *ricoh)
709 struct i2c_client *client = ricoh->client;
711 printk(KERN_INFO "RICOH619 Registers\n");
712 printk(KERN_INFO "------------------\n");
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);
730 static struct ricoh619_platform_data *ricoh619_parse_dt(struct ricoh619 *ricoh619)
732 struct ricoh619_platform_data *pdata;
733 struct device_node *ricoh619_pmic_np;
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");
740 pdata = devm_kzalloc(ricoh619->dev, sizeof(*pdata), GFP_KERNEL);
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);
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);
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);
759 pdata->pmic_sleep = true;
761 pdata->pm_off = of_property_read_bool(ricoh619_pmic_np,"ricoh619,system-power-controller");
767 static struct ricoh619_platform_data *ricoh619_parse_dt(struct ricoh619 *ricoh619)
773 static void ricoh619_noe_init(struct ricoh619 *ricoh)
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
785 static int ricoh619_pre_init(struct ricoh619 *ricoh619)
788 printk("%s,line=%d\n", __func__,__LINE__);
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);
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
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
812 static int ricoh619_i2c_probe(struct i2c_client *client,
813 const struct i2c_device_id *id)
815 struct ricoh619 *ricoh619;
816 struct ricoh619_platform_data *pdata;
820 printk("%s,line=%d\n", __func__,__LINE__);
822 ricoh619 = devm_kzalloc(&client->dev,sizeof(struct ricoh619), GFP_KERNEL);
823 if (ricoh619 == NULL)
826 ricoh619->client = client;
827 ricoh619->dev = &client->dev;
828 i2c_set_clientdata(client, ricoh619);
829 mutex_init(&ricoh619->io_lock);
831 ret = ricoh619_read(ricoh619->dev, 0x36, &control);
832 if ((ret <0) || (control < 0) || (control == 0xff) || (control == 0) ){
834 printk(KERN_INFO "The device is not ricoh619 %08x %d\n",control,ret);
839 ret = ricoh619_write(ricoh619->dev, 0xff, 0x00);
840 ret = ricoh619_read(ricoh619->dev, 0x36, &control);
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) ){
846 printk(KERN_INFO "##################The device is not ricoh619 %08x %d\n",control,ret);
852 ret = ricoh619_pre_init(ricoh619);
854 printk("The ricoh619_pre_init failed %d\n",ret);
858 ricoh619->bank_num = 0;
860 if (ricoh619->dev->of_node)
861 pdata = ricoh619_parse_dt(ricoh619);
864 ricoh619->dc_det = pdata->dc_det;
866 /******************************set sleep vol & dcdc mode******************/
868 if (pdata->pmic_sleep_gpio) {
869 ret = gpio_request(pdata->pmic_sleep_gpio, "ricoh619_pmic_sleep");
871 dev_err(ricoh619->dev,"Failed to request gpio %d with ret:""%d\n", pdata->pmic_sleep_gpio, ret);
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);
880 /**********************************************************/
881 ret = ricoh619_irq_init(ricoh619, pdata->irq_gpio, pdata);
885 ret = mfd_add_devices(ricoh619->dev, -1,
886 ricoh619s, ARRAY_SIZE(ricoh619s),
888 g_ricoh619 = ricoh619;
889 if (pdata->pm_off && !pm_power_off) {
890 pm_power_off = ricoh619_power_off;
892 ricoh619_debuginit(ricoh619);
894 ricoh619_i2c_client = client;
897 mfd_remove_devices(ricoh619->dev);
901 static int ricoh619_i2c_remove(struct i2c_client *client)
903 struct ricoh619 *ricoh619 = i2c_get_clientdata(client);
904 ricoh619_remove_subdevs(ricoh619);
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)
913 // printk("PMU: %s: \n",__func__);
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);
922 static int ricoh619_i2c_resume(struct i2c_client *client)
927 ret = __ricoh619_read(client, RICOH619_INT_IR_SYS, ®_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
935 if (g_ricoh619->chip_irq)
936 enable_irq(g_ricoh619->chip_irq);
940 static int ricoh619_i2c_late_suspend(struct device *dev)
942 struct i2c_client *client = i2c_verify_client(dev);
944 ricoh619_i2c_suspend(client,PMSG_SUSPEND);
948 static int rockchip_i2c_late_resume(struct device *dev)
950 struct i2c_client *client = i2c_verify_client(dev);
952 ricoh619_i2c_resume(client);
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,
963 static const struct i2c_device_id ricoh619_i2c_id[] = {
967 MODULE_DEVICE_TABLE(i2c, ricoh619_i2c_id);
970 static const struct of_device_id ricoh619_dt_match[] = {
971 { .compatible = "ricoh,ricoh619", },
974 MODULE_DEVICE_TABLE(of, ricoh619_dt_match);
977 static struct i2c_driver ricoh619_i2c_driver = {
980 .owner = THIS_MODULE,
982 .pm = (&ricoh619_i2c_dev_pm),
984 .of_match_table = of_match_ptr(ricoh619_dt_match),
986 .probe = ricoh619_i2c_probe,
987 .remove = ricoh619_i2c_remove,
988 .shutdown = ricoh619_device_shutdown,
990 .id_table = ricoh619_i2c_id,
994 static int __init ricoh619_i2c_init(void)
997 ret = i2c_add_driver(&ricoh619_i2c_driver);
999 pr_err("Failed to register I2C driver: %d\n", ret);
1004 subsys_initcall_sync(ricoh619_i2c_init);
1006 static void __exit ricoh619_i2c_exit(void)
1008 i2c_del_driver(&ricoh619_i2c_driver);
1011 module_exit(ricoh619_i2c_exit);
1013 MODULE_DESCRIPTION("RICOH RC5T619 PMU multi-function core driver");
1014 MODULE_AUTHOR("zhangqing <zhangqing@rock-chips.com>");
1015 MODULE_LICENSE("GPL");