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-muic", },
49 .name = "max77693-haptic",
50 .of_compatible = "maxim,max77693-haptic",
53 .name = "max77693-flash",
54 .of_compatible = "maxim,max77693-flash",
58 static const struct regmap_config max77693_regmap_config = {
61 .max_register = MAX77693_PMIC_REG_END,
64 static const struct regmap_irq max77693_led_irqs[] = {
65 { .mask = LED_IRQ_FLED2_OPEN, },
66 { .mask = LED_IRQ_FLED2_SHORT, },
67 { .mask = LED_IRQ_FLED1_OPEN, },
68 { .mask = LED_IRQ_FLED1_SHORT, },
69 { .mask = LED_IRQ_MAX_FLASH, },
72 static const struct regmap_irq_chip max77693_led_irq_chip = {
73 .name = "max77693-led",
74 .status_base = MAX77693_LED_REG_FLASH_INT,
75 .mask_base = MAX77693_LED_REG_FLASH_INT_MASK,
78 .irqs = max77693_led_irqs,
79 .num_irqs = ARRAY_SIZE(max77693_led_irqs),
82 static const struct regmap_irq max77693_topsys_irqs[] = {
83 { .mask = TOPSYS_IRQ_T120C_INT, },
84 { .mask = TOPSYS_IRQ_T140C_INT, },
85 { .mask = TOPSYS_IRQ_LOWSYS_INT, },
88 static const struct regmap_irq_chip max77693_topsys_irq_chip = {
89 .name = "max77693-topsys",
90 .status_base = MAX77693_PMIC_REG_TOPSYS_INT,
91 .mask_base = MAX77693_PMIC_REG_TOPSYS_INT_MASK,
94 .irqs = max77693_topsys_irqs,
95 .num_irqs = ARRAY_SIZE(max77693_topsys_irqs),
98 static const struct regmap_irq max77693_charger_irqs[] = {
99 { .mask = CHG_IRQ_BYP_I, },
100 { .mask = CHG_IRQ_THM_I, },
101 { .mask = CHG_IRQ_BAT_I, },
102 { .mask = CHG_IRQ_CHG_I, },
103 { .mask = CHG_IRQ_CHGIN_I, },
106 static const struct regmap_irq_chip max77693_charger_irq_chip = {
107 .name = "max77693-charger",
108 .status_base = MAX77693_CHG_REG_CHG_INT,
109 .mask_base = MAX77693_CHG_REG_CHG_INT_MASK,
110 .mask_invert = false,
112 .irqs = max77693_charger_irqs,
113 .num_irqs = ARRAY_SIZE(max77693_charger_irqs),
116 static const struct regmap_config max77693_regmap_muic_config = {
119 .max_register = MAX77693_MUIC_REG_END,
122 static const struct regmap_irq max77693_muic_irqs[] = {
123 { .reg_offset = 0, .mask = MUIC_IRQ_INT1_ADC, },
124 { .reg_offset = 0, .mask = MUIC_IRQ_INT1_ADC_LOW, },
125 { .reg_offset = 0, .mask = MUIC_IRQ_INT1_ADC_ERR, },
126 { .reg_offset = 0, .mask = MUIC_IRQ_INT1_ADC1K, },
128 { .reg_offset = 1, .mask = MUIC_IRQ_INT2_CHGTYP, },
129 { .reg_offset = 1, .mask = MUIC_IRQ_INT2_CHGDETREUN, },
130 { .reg_offset = 1, .mask = MUIC_IRQ_INT2_DCDTMR, },
131 { .reg_offset = 1, .mask = MUIC_IRQ_INT2_DXOVP, },
132 { .reg_offset = 1, .mask = MUIC_IRQ_INT2_VBVOLT, },
133 { .reg_offset = 1, .mask = MUIC_IRQ_INT2_VIDRM, },
135 { .reg_offset = 2, .mask = MUIC_IRQ_INT3_EOC, },
136 { .reg_offset = 2, .mask = MUIC_IRQ_INT3_CGMBC, },
137 { .reg_offset = 2, .mask = MUIC_IRQ_INT3_OVP, },
138 { .reg_offset = 2, .mask = MUIC_IRQ_INT3_MBCCHG_ERR, },
139 { .reg_offset = 2, .mask = MUIC_IRQ_INT3_CHG_ENABLED, },
140 { .reg_offset = 2, .mask = MUIC_IRQ_INT3_BAT_DET, },
143 static const struct regmap_irq_chip max77693_muic_irq_chip = {
144 .name = "max77693-muic",
145 .status_base = MAX77693_MUIC_REG_INT1,
146 .mask_base = MAX77693_MUIC_REG_INTMASK1,
149 .irqs = max77693_muic_irqs,
150 .num_irqs = ARRAY_SIZE(max77693_muic_irqs),
153 static const struct regmap_config max77693_regmap_haptic_config = {
156 .max_register = MAX77693_HAPTIC_REG_END,
159 static int max77693_i2c_probe(struct i2c_client *i2c,
160 const struct i2c_device_id *id)
162 struct max77693_dev *max77693;
163 unsigned int reg_data;
166 max77693 = devm_kzalloc(&i2c->dev,
167 sizeof(struct max77693_dev), GFP_KERNEL);
168 if (max77693 == NULL)
171 i2c_set_clientdata(i2c, max77693);
172 max77693->dev = &i2c->dev;
174 max77693->irq = i2c->irq;
175 max77693->type = id->driver_data;
177 max77693->regmap = devm_regmap_init_i2c(i2c, &max77693_regmap_config);
178 if (IS_ERR(max77693->regmap)) {
179 ret = PTR_ERR(max77693->regmap);
180 dev_err(max77693->dev, "failed to allocate register map: %d\n",
185 ret = regmap_read(max77693->regmap, MAX77693_PMIC_REG_PMIC_ID2,
188 dev_err(max77693->dev, "device not found on this channel\n");
191 dev_info(max77693->dev, "device ID: 0x%x\n", reg_data);
193 max77693->muic = i2c_new_dummy(i2c->adapter, I2C_ADDR_MUIC);
194 if (!max77693->muic) {
195 dev_err(max77693->dev, "Failed to allocate I2C device for MUIC\n");
198 i2c_set_clientdata(max77693->muic, max77693);
200 max77693->haptic = i2c_new_dummy(i2c->adapter, I2C_ADDR_HAPTIC);
201 if (!max77693->haptic) {
202 dev_err(max77693->dev, "Failed to allocate I2C device for Haptic\n");
206 i2c_set_clientdata(max77693->haptic, max77693);
208 max77693->regmap_haptic = devm_regmap_init_i2c(max77693->haptic,
209 &max77693_regmap_haptic_config);
210 if (IS_ERR(max77693->regmap_haptic)) {
211 ret = PTR_ERR(max77693->regmap_haptic);
212 dev_err(max77693->dev,
213 "failed to initialize haptic register map: %d\n", ret);
218 * Initialize register map for MUIC device because use regmap-muic
219 * instance of MUIC device when irq of max77693 is initialized
220 * before call max77693-muic probe() function.
222 max77693->regmap_muic = devm_regmap_init_i2c(max77693->muic,
223 &max77693_regmap_muic_config);
224 if (IS_ERR(max77693->regmap_muic)) {
225 ret = PTR_ERR(max77693->regmap_muic);
226 dev_err(max77693->dev,
227 "failed to allocate register map: %d\n", ret);
231 ret = regmap_add_irq_chip(max77693->regmap, max77693->irq,
232 IRQF_ONESHOT | IRQF_SHARED |
233 IRQF_TRIGGER_FALLING, 0,
234 &max77693_led_irq_chip,
235 &max77693->irq_data_led);
237 dev_err(max77693->dev, "failed to add irq chip: %d\n", ret);
241 ret = regmap_add_irq_chip(max77693->regmap, max77693->irq,
242 IRQF_ONESHOT | IRQF_SHARED |
243 IRQF_TRIGGER_FALLING, 0,
244 &max77693_topsys_irq_chip,
245 &max77693->irq_data_topsys);
247 dev_err(max77693->dev, "failed to add irq chip: %d\n", ret);
251 ret = regmap_add_irq_chip(max77693->regmap, max77693->irq,
252 IRQF_ONESHOT | IRQF_SHARED |
253 IRQF_TRIGGER_FALLING, 0,
254 &max77693_charger_irq_chip,
255 &max77693->irq_data_charger);
257 dev_err(max77693->dev, "failed to add irq chip: %d\n", ret);
258 goto err_irq_charger;
261 ret = regmap_add_irq_chip(max77693->regmap, max77693->irq,
262 IRQF_ONESHOT | IRQF_SHARED |
263 IRQF_TRIGGER_FALLING, 0,
264 &max77693_muic_irq_chip,
265 &max77693->irq_data_muic);
267 dev_err(max77693->dev, "failed to add irq chip: %d\n", ret);
271 pm_runtime_set_active(max77693->dev);
273 ret = mfd_add_devices(max77693->dev, -1, max77693_devs,
274 ARRAY_SIZE(max77693_devs), NULL, 0, NULL);
281 mfd_remove_devices(max77693->dev);
282 regmap_del_irq_chip(max77693->irq, max77693->irq_data_muic);
284 regmap_del_irq_chip(max77693->irq, max77693->irq_data_charger);
286 regmap_del_irq_chip(max77693->irq, max77693->irq_data_topsys);
288 regmap_del_irq_chip(max77693->irq, max77693->irq_data_led);
290 i2c_unregister_device(max77693->haptic);
292 i2c_unregister_device(max77693->muic);
296 static int max77693_i2c_remove(struct i2c_client *i2c)
298 struct max77693_dev *max77693 = i2c_get_clientdata(i2c);
300 mfd_remove_devices(max77693->dev);
302 regmap_del_irq_chip(max77693->irq, max77693->irq_data_muic);
303 regmap_del_irq_chip(max77693->irq, max77693->irq_data_charger);
304 regmap_del_irq_chip(max77693->irq, max77693->irq_data_topsys);
305 regmap_del_irq_chip(max77693->irq, max77693->irq_data_led);
307 i2c_unregister_device(max77693->muic);
308 i2c_unregister_device(max77693->haptic);
313 static const struct i2c_device_id max77693_i2c_id[] = {
314 { "max77693", TYPE_MAX77693 },
317 MODULE_DEVICE_TABLE(i2c, max77693_i2c_id);
319 static int max77693_suspend(struct device *dev)
321 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
322 struct max77693_dev *max77693 = i2c_get_clientdata(i2c);
324 if (device_may_wakeup(dev)) {
325 enable_irq_wake(max77693->irq);
326 disable_irq(max77693->irq);
332 static int max77693_resume(struct device *dev)
334 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
335 struct max77693_dev *max77693 = i2c_get_clientdata(i2c);
337 if (device_may_wakeup(dev)) {
338 disable_irq_wake(max77693->irq);
339 enable_irq(max77693->irq);
345 static const struct dev_pm_ops max77693_pm = {
346 .suspend = max77693_suspend,
347 .resume = max77693_resume,
351 static const struct of_device_id max77693_dt_match[] = {
352 { .compatible = "maxim,max77693" },
357 static struct i2c_driver max77693_i2c_driver = {
360 .owner = THIS_MODULE,
362 .of_match_table = of_match_ptr(max77693_dt_match),
364 .probe = max77693_i2c_probe,
365 .remove = max77693_i2c_remove,
366 .id_table = max77693_i2c_id,
369 static int __init max77693_i2c_init(void)
371 return i2c_add_driver(&max77693_i2c_driver);
373 /* init early so consumer devices can complete system boot */
374 subsys_initcall(max77693_i2c_init);
376 static void __exit max77693_i2c_exit(void)
378 i2c_del_driver(&max77693_i2c_driver);
380 module_exit(max77693_i2c_exit);
382 MODULE_DESCRIPTION("MAXIM 77693 multi-function core driver");
383 MODULE_AUTHOR("SangYoung, Son <hello.son@samsung.com>");
384 MODULE_LICENSE("GPL");