2 * max77693.c - mfd core driver for the MAX 77693
4 * Copyright (C) 2012 Samsung Electronics
5 * SangYoung Son <hello.son@smasung.com>
7 * This program is not provided / owned by Maxim Integrated Products.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * This driver is based on max8997.c
26 #include <linux/module.h>
27 #include <linux/slab.h>
28 #include <linux/i2c.h>
29 #include <linux/err.h>
30 #include <linux/interrupt.h>
32 #include <linux/pm_runtime.h>
33 #include <linux/mutex.h>
34 #include <linux/mfd/core.h>
35 #include <linux/mfd/max77693.h>
36 #include <linux/mfd/max77693-private.h>
37 #include <linux/regulator/machine.h>
38 #include <linux/regmap.h>
40 #define I2C_ADDR_PMIC (0xCC >> 1) /* Charger, Flash LED */
41 #define I2C_ADDR_MUIC (0x4A >> 1)
42 #define I2C_ADDR_HAPTIC (0x90 >> 1)
44 static const struct mfd_cell max77693_devs[] = {
45 { .name = "max77693-pmic", },
46 { .name = "max77693-charger", },
47 { .name = "max77693-flash", },
48 { .name = "max77693-muic", },
49 { .name = "max77693-haptic", },
52 static const struct regmap_config max77693_regmap_config = {
55 .max_register = MAX77693_PMIC_REG_END,
58 static const struct regmap_irq max77693_led_irqs[] = {
59 { .mask = LED_IRQ_FLED2_OPEN, },
60 { .mask = LED_IRQ_FLED2_SHORT, },
61 { .mask = LED_IRQ_FLED1_OPEN, },
62 { .mask = LED_IRQ_FLED1_SHORT, },
63 { .mask = LED_IRQ_MAX_FLASH, },
66 static const struct regmap_irq_chip max77693_led_irq_chip = {
67 .name = "max77693-led",
68 .status_base = MAX77693_LED_REG_FLASH_INT,
69 .mask_base = MAX77693_LED_REG_FLASH_INT_MASK,
72 .irqs = max77693_led_irqs,
73 .num_irqs = ARRAY_SIZE(max77693_led_irqs),
76 static const struct regmap_irq max77693_topsys_irqs[] = {
77 { .mask = TOPSYS_IRQ_T120C_INT, },
78 { .mask = TOPSYS_IRQ_T140C_INT, },
79 { .mask = TOPSYS_IRQ_LOWSYS_INT, },
82 static const struct regmap_irq_chip max77693_topsys_irq_chip = {
83 .name = "max77693-topsys",
84 .status_base = MAX77693_PMIC_REG_TOPSYS_INT,
85 .mask_base = MAX77693_PMIC_REG_TOPSYS_INT_MASK,
88 .irqs = max77693_topsys_irqs,
89 .num_irqs = ARRAY_SIZE(max77693_topsys_irqs),
92 static const struct regmap_irq max77693_charger_irqs[] = {
93 { .mask = CHG_IRQ_BYP_I, },
94 { .mask = CHG_IRQ_THM_I, },
95 { .mask = CHG_IRQ_BAT_I, },
96 { .mask = CHG_IRQ_CHG_I, },
97 { .mask = CHG_IRQ_CHGIN_I, },
100 static const struct regmap_irq_chip max77693_charger_irq_chip = {
101 .name = "max77693-charger",
102 .status_base = MAX77693_CHG_REG_CHG_INT,
103 .mask_base = MAX77693_CHG_REG_CHG_INT_MASK,
104 .mask_invert = false,
106 .irqs = max77693_charger_irqs,
107 .num_irqs = ARRAY_SIZE(max77693_charger_irqs),
110 static const struct regmap_config max77693_regmap_muic_config = {
113 .max_register = MAX77693_MUIC_REG_END,
116 static const struct regmap_irq max77693_muic_irqs[] = {
117 { .reg_offset = 0, .mask = MUIC_IRQ_INT1_ADC, },
118 { .reg_offset = 0, .mask = MUIC_IRQ_INT1_ADC_LOW, },
119 { .reg_offset = 0, .mask = MUIC_IRQ_INT1_ADC_ERR, },
120 { .reg_offset = 0, .mask = MUIC_IRQ_INT1_ADC1K, },
122 { .reg_offset = 1, .mask = MUIC_IRQ_INT2_CHGTYP, },
123 { .reg_offset = 1, .mask = MUIC_IRQ_INT2_CHGDETREUN, },
124 { .reg_offset = 1, .mask = MUIC_IRQ_INT2_DCDTMR, },
125 { .reg_offset = 1, .mask = MUIC_IRQ_INT2_DXOVP, },
126 { .reg_offset = 1, .mask = MUIC_IRQ_INT2_VBVOLT, },
127 { .reg_offset = 1, .mask = MUIC_IRQ_INT2_VIDRM, },
129 { .reg_offset = 2, .mask = MUIC_IRQ_INT3_EOC, },
130 { .reg_offset = 2, .mask = MUIC_IRQ_INT3_CGMBC, },
131 { .reg_offset = 2, .mask = MUIC_IRQ_INT3_OVP, },
132 { .reg_offset = 2, .mask = MUIC_IRQ_INT3_MBCCHG_ERR, },
133 { .reg_offset = 2, .mask = MUIC_IRQ_INT3_CHG_ENABLED, },
134 { .reg_offset = 2, .mask = MUIC_IRQ_INT3_BAT_DET, },
137 static const struct regmap_irq_chip max77693_muic_irq_chip = {
138 .name = "max77693-muic",
139 .status_base = MAX77693_MUIC_REG_INT1,
140 .mask_base = MAX77693_MUIC_REG_INTMASK1,
143 .irqs = max77693_muic_irqs,
144 .num_irqs = ARRAY_SIZE(max77693_muic_irqs),
147 static int max77693_i2c_probe(struct i2c_client *i2c,
148 const struct i2c_device_id *id)
150 struct max77693_dev *max77693;
151 unsigned int reg_data;
154 max77693 = devm_kzalloc(&i2c->dev,
155 sizeof(struct max77693_dev), GFP_KERNEL);
156 if (max77693 == NULL)
159 i2c_set_clientdata(i2c, max77693);
160 max77693->dev = &i2c->dev;
162 max77693->irq = i2c->irq;
163 max77693->type = id->driver_data;
165 max77693->regmap = devm_regmap_init_i2c(i2c, &max77693_regmap_config);
166 if (IS_ERR(max77693->regmap)) {
167 ret = PTR_ERR(max77693->regmap);
168 dev_err(max77693->dev, "failed to allocate register map: %d\n",
173 ret = regmap_read(max77693->regmap, MAX77693_PMIC_REG_PMIC_ID2,
176 dev_err(max77693->dev, "device not found on this channel\n");
179 dev_info(max77693->dev, "device ID: 0x%x\n", reg_data);
181 max77693->muic = i2c_new_dummy(i2c->adapter, I2C_ADDR_MUIC);
182 if (!max77693->muic) {
183 dev_err(max77693->dev, "Failed to allocate I2C device for MUIC\n");
186 i2c_set_clientdata(max77693->muic, max77693);
188 max77693->haptic = i2c_new_dummy(i2c->adapter, I2C_ADDR_HAPTIC);
189 if (!max77693->haptic) {
190 dev_err(max77693->dev, "Failed to allocate I2C device for Haptic\n");
194 i2c_set_clientdata(max77693->haptic, max77693);
197 * Initialize register map for MUIC device because use regmap-muic
198 * instance of MUIC device when irq of max77693 is initialized
199 * before call max77693-muic probe() function.
201 max77693->regmap_muic = devm_regmap_init_i2c(max77693->muic,
202 &max77693_regmap_muic_config);
203 if (IS_ERR(max77693->regmap_muic)) {
204 ret = PTR_ERR(max77693->regmap_muic);
205 dev_err(max77693->dev,
206 "failed to allocate register map: %d\n", ret);
207 goto err_regmap_muic;
210 ret = regmap_add_irq_chip(max77693->regmap, max77693->irq,
211 IRQF_ONESHOT | IRQF_SHARED |
212 IRQF_TRIGGER_FALLING, 0,
213 &max77693_led_irq_chip,
214 &max77693->irq_data_led);
216 dev_err(max77693->dev, "failed to add irq chip: %d\n", ret);
217 goto err_regmap_muic;
220 ret = regmap_add_irq_chip(max77693->regmap, max77693->irq,
221 IRQF_ONESHOT | IRQF_SHARED |
222 IRQF_TRIGGER_FALLING, 0,
223 &max77693_topsys_irq_chip,
224 &max77693->irq_data_topsys);
226 dev_err(max77693->dev, "failed to add irq chip: %d\n", ret);
230 ret = regmap_add_irq_chip(max77693->regmap, max77693->irq,
231 IRQF_ONESHOT | IRQF_SHARED |
232 IRQF_TRIGGER_FALLING, 0,
233 &max77693_charger_irq_chip,
234 &max77693->irq_data_charger);
236 dev_err(max77693->dev, "failed to add irq chip: %d\n", ret);
237 goto err_irq_charger;
240 ret = regmap_add_irq_chip(max77693->regmap, max77693->irq,
241 IRQF_ONESHOT | IRQF_SHARED |
242 IRQF_TRIGGER_FALLING, 0,
243 &max77693_muic_irq_chip,
244 &max77693->irq_data_muic);
246 dev_err(max77693->dev, "failed to add irq chip: %d\n", ret);
250 pm_runtime_set_active(max77693->dev);
252 ret = mfd_add_devices(max77693->dev, -1, max77693_devs,
253 ARRAY_SIZE(max77693_devs), NULL, 0, NULL);
260 mfd_remove_devices(max77693->dev);
261 regmap_del_irq_chip(max77693->irq, max77693->irq_data_muic);
263 regmap_del_irq_chip(max77693->irq, max77693->irq_data_charger);
265 regmap_del_irq_chip(max77693->irq, max77693->irq_data_topsys);
267 regmap_del_irq_chip(max77693->irq, max77693->irq_data_led);
269 i2c_unregister_device(max77693->haptic);
271 i2c_unregister_device(max77693->muic);
275 static int max77693_i2c_remove(struct i2c_client *i2c)
277 struct max77693_dev *max77693 = i2c_get_clientdata(i2c);
279 mfd_remove_devices(max77693->dev);
281 regmap_del_irq_chip(max77693->irq, max77693->irq_data_muic);
282 regmap_del_irq_chip(max77693->irq, max77693->irq_data_charger);
283 regmap_del_irq_chip(max77693->irq, max77693->irq_data_topsys);
284 regmap_del_irq_chip(max77693->irq, max77693->irq_data_led);
286 i2c_unregister_device(max77693->muic);
287 i2c_unregister_device(max77693->haptic);
292 static const struct i2c_device_id max77693_i2c_id[] = {
293 { "max77693", TYPE_MAX77693 },
296 MODULE_DEVICE_TABLE(i2c, max77693_i2c_id);
298 static int max77693_suspend(struct device *dev)
300 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
301 struct max77693_dev *max77693 = i2c_get_clientdata(i2c);
303 if (device_may_wakeup(dev)) {
304 enable_irq_wake(max77693->irq);
305 disable_irq(max77693->irq);
311 static int max77693_resume(struct device *dev)
313 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
314 struct max77693_dev *max77693 = i2c_get_clientdata(i2c);
316 if (device_may_wakeup(dev)) {
317 disable_irq_wake(max77693->irq);
318 enable_irq(max77693->irq);
324 static const struct dev_pm_ops max77693_pm = {
325 .suspend = max77693_suspend,
326 .resume = max77693_resume,
330 static const struct of_device_id max77693_dt_match[] = {
331 { .compatible = "maxim,max77693" },
336 static struct i2c_driver max77693_i2c_driver = {
339 .owner = THIS_MODULE,
341 .of_match_table = of_match_ptr(max77693_dt_match),
343 .probe = max77693_i2c_probe,
344 .remove = max77693_i2c_remove,
345 .id_table = max77693_i2c_id,
348 static int __init max77693_i2c_init(void)
350 return i2c_add_driver(&max77693_i2c_driver);
352 /* init early so consumer devices can complete system boot */
353 subsys_initcall(max77693_i2c_init);
355 static void __exit max77693_i2c_exit(void)
357 i2c_del_driver(&max77693_i2c_driver);
359 module_exit(max77693_i2c_exit);
361 MODULE_DESCRIPTION("MAXIM 77693 multi-function core driver");
362 MODULE_AUTHOR("SangYoung, Son <hello.son@samsung.com>");
363 MODULE_LICENSE("GPL");