b57ae93b1cbafb8731f287cf36e65909499ca912
[firefly-linux-kernel-4.4.55.git] / drivers / mfd / max77693.c
1 /*
2  * max77693.c - mfd core driver for the MAX 77693
3  *
4  * Copyright (C) 2012 Samsung Electronics
5  * SangYoung Son <hello.son@smasung.com>
6  *
7  * This program is not provided / owned by Maxim Integrated Products.
8  *
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.
13  *
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.
18  *
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
22  *
23  * This driver is based on max8997.c
24  */
25
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>
31 #include <linux/of.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>
39
40 #define I2C_ADDR_PMIC   (0xCC >> 1)     /* Charger, Flash LED */
41 #define I2C_ADDR_MUIC   (0x4A >> 1)
42 #define I2C_ADDR_HAPTIC (0x90 >> 1)
43
44 static const struct mfd_cell max77693_devs[] = {
45         { .name = "max77693-pmic", },
46         { .name = "max77693-charger", },
47         { .name = "max77693-muic", },
48         {
49                 .name = "max77693-haptic",
50                 .of_compatible = "maxim,max77693-haptic",
51         },
52         {
53                 .name = "max77693-flash",
54                 .of_compatible = "maxim,max77693-flash",
55         },
56 };
57
58 static const struct regmap_config max77693_regmap_config = {
59         .reg_bits = 8,
60         .val_bits = 8,
61         .max_register = MAX77693_PMIC_REG_END,
62 };
63
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,   },
70 };
71
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,
76         .mask_invert            = false,
77         .num_regs               = 1,
78         .irqs                   = max77693_led_irqs,
79         .num_irqs               = ARRAY_SIZE(max77693_led_irqs),
80 };
81
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, },
86 };
87
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,
92         .mask_invert            = false,
93         .num_regs               = 1,
94         .irqs                   = max77693_topsys_irqs,
95         .num_irqs               = ARRAY_SIZE(max77693_topsys_irqs),
96 };
97
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, },
104 };
105
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,
111         .num_regs               = 1,
112         .irqs                   = max77693_charger_irqs,
113         .num_irqs               = ARRAY_SIZE(max77693_charger_irqs),
114 };
115
116 static const struct regmap_config max77693_regmap_muic_config = {
117         .reg_bits = 8,
118         .val_bits = 8,
119         .max_register = MAX77693_MUIC_REG_END,
120 };
121
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,         },
127
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,         },
134
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,       },
141 };
142
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,
147         .mask_invert            = true,
148         .num_regs               = 3,
149         .irqs                   = max77693_muic_irqs,
150         .num_irqs               = ARRAY_SIZE(max77693_muic_irqs),
151 };
152
153 static const struct regmap_config max77693_regmap_haptic_config = {
154         .reg_bits = 8,
155         .val_bits = 8,
156         .max_register = MAX77693_HAPTIC_REG_END,
157 };
158
159 static int max77693_i2c_probe(struct i2c_client *i2c,
160                               const struct i2c_device_id *id)
161 {
162         struct max77693_dev *max77693;
163         unsigned int reg_data;
164         int ret = 0;
165
166         max77693 = devm_kzalloc(&i2c->dev,
167                         sizeof(struct max77693_dev), GFP_KERNEL);
168         if (max77693 == NULL)
169                 return -ENOMEM;
170
171         i2c_set_clientdata(i2c, max77693);
172         max77693->dev = &i2c->dev;
173         max77693->i2c = i2c;
174         max77693->irq = i2c->irq;
175         max77693->type = id->driver_data;
176
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",
181                                 ret);
182                 return ret;
183         }
184
185         ret = regmap_read(max77693->regmap, MAX77693_PMIC_REG_PMIC_ID2,
186                                 &reg_data);
187         if (ret < 0) {
188                 dev_err(max77693->dev, "device not found on this channel\n");
189                 return ret;
190         } else
191                 dev_info(max77693->dev, "device ID: 0x%x\n", reg_data);
192
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");
196                 return -ENODEV;
197         }
198         i2c_set_clientdata(max77693->muic, max77693);
199
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");
203                 ret = -ENODEV;
204                 goto err_i2c_haptic;
205         }
206         i2c_set_clientdata(max77693->haptic, max77693);
207
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);
214                 goto err_regmap;
215         }
216
217         /*
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.
221          */
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);
228                 goto err_regmap;
229         }
230
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);
236         if (ret) {
237                 dev_err(max77693->dev, "failed to add irq chip: %d\n", ret);
238                 goto err_regmap;
239         }
240
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);
246         if (ret) {
247                 dev_err(max77693->dev, "failed to add irq chip: %d\n", ret);
248                 goto err_irq_topsys;
249         }
250
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);
256         if (ret) {
257                 dev_err(max77693->dev, "failed to add irq chip: %d\n", ret);
258                 goto err_irq_charger;
259         }
260
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);
266         if (ret) {
267                 dev_err(max77693->dev, "failed to add irq chip: %d\n", ret);
268                 goto err_irq_muic;
269         }
270
271         pm_runtime_set_active(max77693->dev);
272
273         ret = mfd_add_devices(max77693->dev, -1, max77693_devs,
274                               ARRAY_SIZE(max77693_devs), NULL, 0, NULL);
275         if (ret < 0)
276                 goto err_mfd;
277
278         return ret;
279
280 err_mfd:
281         mfd_remove_devices(max77693->dev);
282         regmap_del_irq_chip(max77693->irq, max77693->irq_data_muic);
283 err_irq_muic:
284         regmap_del_irq_chip(max77693->irq, max77693->irq_data_charger);
285 err_irq_charger:
286         regmap_del_irq_chip(max77693->irq, max77693->irq_data_topsys);
287 err_irq_topsys:
288         regmap_del_irq_chip(max77693->irq, max77693->irq_data_led);
289 err_regmap:
290         i2c_unregister_device(max77693->haptic);
291 err_i2c_haptic:
292         i2c_unregister_device(max77693->muic);
293         return ret;
294 }
295
296 static int max77693_i2c_remove(struct i2c_client *i2c)
297 {
298         struct max77693_dev *max77693 = i2c_get_clientdata(i2c);
299
300         mfd_remove_devices(max77693->dev);
301
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);
306
307         i2c_unregister_device(max77693->muic);
308         i2c_unregister_device(max77693->haptic);
309
310         return 0;
311 }
312
313 static const struct i2c_device_id max77693_i2c_id[] = {
314         { "max77693", TYPE_MAX77693 },
315         { }
316 };
317 MODULE_DEVICE_TABLE(i2c, max77693_i2c_id);
318
319 static int max77693_suspend(struct device *dev)
320 {
321         struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
322         struct max77693_dev *max77693 = i2c_get_clientdata(i2c);
323
324         if (device_may_wakeup(dev)) {
325                 enable_irq_wake(max77693->irq);
326                 disable_irq(max77693->irq);
327         }
328
329         return 0;
330 }
331
332 static int max77693_resume(struct device *dev)
333 {
334         struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
335         struct max77693_dev *max77693 = i2c_get_clientdata(i2c);
336
337         if (device_may_wakeup(dev)) {
338                 disable_irq_wake(max77693->irq);
339                 enable_irq(max77693->irq);
340         }
341
342         return 0;
343 }
344
345 static const struct dev_pm_ops max77693_pm = {
346         .suspend = max77693_suspend,
347         .resume = max77693_resume,
348 };
349
350 #ifdef CONFIG_OF
351 static const struct of_device_id max77693_dt_match[] = {
352         { .compatible = "maxim,max77693" },
353         {},
354 };
355 #endif
356
357 static struct i2c_driver max77693_i2c_driver = {
358         .driver = {
359                    .name = "max77693",
360                    .owner = THIS_MODULE,
361                    .pm = &max77693_pm,
362                    .of_match_table = of_match_ptr(max77693_dt_match),
363         },
364         .probe = max77693_i2c_probe,
365         .remove = max77693_i2c_remove,
366         .id_table = max77693_i2c_id,
367 };
368
369 static int __init max77693_i2c_init(void)
370 {
371         return i2c_add_driver(&max77693_i2c_driver);
372 }
373 /* init early so consumer devices can complete system boot */
374 subsys_initcall(max77693_i2c_init);
375
376 static void __exit max77693_i2c_exit(void)
377 {
378         i2c_del_driver(&max77693_i2c_driver);
379 }
380 module_exit(max77693_i2c_exit);
381
382 MODULE_DESCRIPTION("MAXIM 77693 multi-function core driver");
383 MODULE_AUTHOR("SangYoung, Son <hello.son@samsung.com>");
384 MODULE_LICENSE("GPL");