2 * max14577.c - mfd core driver for the Maxim 14577
4 * Copyright (C) 2013 Samsung Electrnoics
5 * Chanwoo Choi <cw00.choi@samsung.com>
6 * Krzysztof Kozlowski <k.kozlowski@samsung.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * This driver is based on max8997.c
21 #include <linux/module.h>
22 #include <linux/interrupt.h>
23 #include <linux/mfd/core.h>
24 #include <linux/mfd/max14577.h>
25 #include <linux/mfd/max14577-private.h>
27 static struct mfd_cell max14577_devs[] = {
28 { .name = "max14577-muic", },
29 { .name = "max14577-regulator", },
30 { .name = "max14577-charger", },
33 static bool max14577_volatile_reg(struct device *dev, unsigned int reg)
36 case MAX14577_REG_INT1 ... MAX14577_REG_STATUS3:
44 static const struct regmap_config max14577_regmap_config = {
47 .volatile_reg = max14577_volatile_reg,
48 .max_register = MAX14577_REG_END,
51 static const struct regmap_irq max14577_irqs[] = {
53 { .reg_offset = 0, .mask = INT1_ADC_MASK, },
54 { .reg_offset = 0, .mask = INT1_ADCLOW_MASK, },
55 { .reg_offset = 0, .mask = INT1_ADCERR_MASK, },
57 { .reg_offset = 1, .mask = INT2_CHGTYP_MASK, },
58 { .reg_offset = 1, .mask = INT2_CHGDETRUN_MASK, },
59 { .reg_offset = 1, .mask = INT2_DCDTMR_MASK, },
60 { .reg_offset = 1, .mask = INT2_DBCHG_MASK, },
61 { .reg_offset = 1, .mask = INT2_VBVOLT_MASK, },
63 { .reg_offset = 2, .mask = INT3_EOC_MASK, },
64 { .reg_offset = 2, .mask = INT3_CGMBC_MASK, },
65 { .reg_offset = 2, .mask = INT3_OVP_MASK, },
66 { .reg_offset = 2, .mask = INT3_MBCCHGERR_MASK, },
69 static const struct regmap_irq_chip max14577_irq_chip = {
71 .status_base = MAX14577_REG_INT1,
72 .mask_base = MAX14577_REG_INTMASK1,
75 .irqs = max14577_irqs,
76 .num_irqs = ARRAY_SIZE(max14577_irqs),
79 static int max14577_i2c_probe(struct i2c_client *i2c,
80 const struct i2c_device_id *id)
82 struct max14577 *max14577;
83 struct max14577_platform_data *pdata = dev_get_platdata(&i2c->dev);
84 struct device_node *np = i2c->dev.of_node;
89 pdata = devm_kzalloc(&i2c->dev, sizeof(*pdata), GFP_KERNEL);
92 i2c->dev.platform_data = pdata;
96 dev_err(&i2c->dev, "No platform data found: %ld\n",
101 max14577 = devm_kzalloc(&i2c->dev, sizeof(*max14577), GFP_KERNEL);
105 i2c_set_clientdata(i2c, max14577);
106 max14577->dev = &i2c->dev;
108 max14577->irq = i2c->irq;
110 max14577->regmap = devm_regmap_init_i2c(i2c, &max14577_regmap_config);
111 if (IS_ERR(max14577->regmap)) {
112 ret = PTR_ERR(max14577->regmap);
113 dev_err(max14577->dev, "Failed to allocate register map: %d\n",
118 ret = max14577_read_reg(max14577->regmap, MAX14577_REG_DEVICEID,
121 dev_err(max14577->dev, "Device not found on this channel: %d\n",
125 max14577->vendor_id = ((reg_data & DEVID_VENDORID_MASK) >>
126 DEVID_VENDORID_SHIFT);
127 max14577->device_id = ((reg_data & DEVID_DEVICEID_MASK) >>
128 DEVID_DEVICEID_SHIFT);
129 dev_info(max14577->dev, "Device ID: 0x%x, vendor: 0x%x\n",
130 max14577->device_id, max14577->vendor_id);
132 ret = regmap_add_irq_chip(max14577->regmap, max14577->irq,
133 IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 0,
135 &max14577->irq_data);
137 dev_err(&i2c->dev, "Failed to request IRQ %d: %d\n",
142 ret = mfd_add_devices(max14577->dev, -1, max14577_devs,
143 ARRAY_SIZE(max14577_devs), NULL, 0,
144 regmap_irq_get_domain(max14577->irq_data));
148 device_init_wakeup(max14577->dev, 1);
153 regmap_del_irq_chip(max14577->irq, max14577->irq_data);
158 static int max14577_i2c_remove(struct i2c_client *i2c)
160 struct max14577 *max14577 = i2c_get_clientdata(i2c);
162 mfd_remove_devices(max14577->dev);
163 regmap_del_irq_chip(max14577->irq, max14577->irq_data);
168 static const struct i2c_device_id max14577_i2c_id[] = {
172 MODULE_DEVICE_TABLE(i2c, max14577_i2c_id);
174 static int max14577_suspend(struct device *dev)
176 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
177 struct max14577 *max14577 = i2c_get_clientdata(i2c);
179 if (device_may_wakeup(dev)) {
180 enable_irq_wake(max14577->irq);
182 * MUIC IRQ must be disabled during suspend if this is
183 * a wake up source because it will be handled before
186 * When device is woken up from suspend (e.g. by ADC change),
187 * an interrupt occurs before resuming I2C bus controller.
188 * Interrupt handler tries to read registers but this read
189 * will fail because I2C is still suspended.
191 disable_irq(max14577->irq);
197 static int max14577_resume(struct device *dev)
199 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
200 struct max14577 *max14577 = i2c_get_clientdata(i2c);
202 if (device_may_wakeup(dev)) {
203 disable_irq_wake(max14577->irq);
204 enable_irq(max14577->irq);
210 static struct of_device_id max14577_dt_match[] = {
211 { .compatible = "maxim,max14577", },
215 static SIMPLE_DEV_PM_OPS(max14577_pm, max14577_suspend, max14577_resume);
217 static struct i2c_driver max14577_i2c_driver = {
220 .owner = THIS_MODULE,
222 .of_match_table = of_match_ptr(max14577_dt_match),
224 .probe = max14577_i2c_probe,
225 .remove = max14577_i2c_remove,
226 .id_table = max14577_i2c_id,
229 static int __init max14577_i2c_init(void)
231 return i2c_add_driver(&max14577_i2c_driver);
233 subsys_initcall(max14577_i2c_init);
235 static void __exit max14577_i2c_exit(void)
237 i2c_del_driver(&max14577_i2c_driver);
239 module_exit(max14577_i2c_exit);
241 MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>, Krzysztof Kozlowski <k.kozlowski@samsung.com>");
242 MODULE_DESCRIPTION("MAXIM 14577 multi-function core driver");
243 MODULE_LICENSE("GPL");