blob: 007f729e150b0c191d8d796fc6e6edb923d7a6e3 [file] [log] [blame]
Chanwoo Choi83871c02012-05-14 22:50:39 +02001/*
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>
Sachin Kamat71c7f932013-10-18 16:11:57 +053031#include <linux/of.h>
Chanwoo Choi83871c02012-05-14 22:50:39 +020032#include <linux/pm_runtime.h>
33#include <linux/mutex.h>
34#include <linux/mfd/core.h>
35#include <linux/mfd/max77693.h>
Krzysztof Kozlowski61b305c2015-07-15 21:59:50 +090036#include <linux/mfd/max77693-common.h>
Chanwoo Choi83871c02012-05-14 22:50:39 +020037#include <linux/mfd/max77693-private.h>
38#include <linux/regulator/machine.h>
39#include <linux/regmap.h>
40
41#define I2C_ADDR_PMIC (0xCC >> 1) /* Charger, Flash LED */
42#define I2C_ADDR_MUIC (0x4A >> 1)
43#define I2C_ADDR_HAPTIC (0x90 >> 1)
44
Geert Uytterhoeven7c0517b2013-11-18 14:33:00 +010045static const struct mfd_cell max77693_devs[] = {
Chanwoo Choi83871c02012-05-14 22:50:39 +020046 { .name = "max77693-pmic", },
Krzysztof Kozlowskiefa3ca42014-10-20 14:34:46 +020047 {
48 .name = "max77693-charger",
49 .of_compatible = "maxim,max77693-charger",
50 },
Chanwoo Choi83871c02012-05-14 22:50:39 +020051 { .name = "max77693-muic", },
Jaewon Kimd1bafd72014-09-18 01:40:25 +090052 {
53 .name = "max77693-haptic",
54 .of_compatible = "maxim,max77693-haptic",
55 },
Jacek Anaszewskia0bc60722014-08-22 11:06:18 +020056 {
Jacek Anaszewski224995d2015-03-04 17:14:26 +010057 .name = "max77693-led",
58 .of_compatible = "maxim,max77693-led",
Jacek Anaszewskia0bc60722014-08-22 11:06:18 +020059 },
Chanwoo Choi83871c02012-05-14 22:50:39 +020060};
61
Chanwoo Choi83871c02012-05-14 22:50:39 +020062static const struct regmap_config max77693_regmap_config = {
63 .reg_bits = 8,
64 .val_bits = 8,
65 .max_register = MAX77693_PMIC_REG_END,
66};
67
Robert Baldyga342d6692014-05-21 08:52:48 +020068static const struct regmap_irq max77693_led_irqs[] = {
69 { .mask = LED_IRQ_FLED2_OPEN, },
70 { .mask = LED_IRQ_FLED2_SHORT, },
71 { .mask = LED_IRQ_FLED1_OPEN, },
72 { .mask = LED_IRQ_FLED1_SHORT, },
73 { .mask = LED_IRQ_MAX_FLASH, },
74};
75
76static const struct regmap_irq_chip max77693_led_irq_chip = {
77 .name = "max77693-led",
78 .status_base = MAX77693_LED_REG_FLASH_INT,
79 .mask_base = MAX77693_LED_REG_FLASH_INT_MASK,
80 .mask_invert = false,
81 .num_regs = 1,
82 .irqs = max77693_led_irqs,
83 .num_irqs = ARRAY_SIZE(max77693_led_irqs),
84};
85
86static const struct regmap_irq max77693_topsys_irqs[] = {
87 { .mask = TOPSYS_IRQ_T120C_INT, },
88 { .mask = TOPSYS_IRQ_T140C_INT, },
89 { .mask = TOPSYS_IRQ_LOWSYS_INT, },
90};
91
92static const struct regmap_irq_chip max77693_topsys_irq_chip = {
93 .name = "max77693-topsys",
94 .status_base = MAX77693_PMIC_REG_TOPSYS_INT,
95 .mask_base = MAX77693_PMIC_REG_TOPSYS_INT_MASK,
96 .mask_invert = false,
97 .num_regs = 1,
98 .irqs = max77693_topsys_irqs,
99 .num_irqs = ARRAY_SIZE(max77693_topsys_irqs),
100};
101
102static const struct regmap_irq max77693_charger_irqs[] = {
103 { .mask = CHG_IRQ_BYP_I, },
104 { .mask = CHG_IRQ_THM_I, },
105 { .mask = CHG_IRQ_BAT_I, },
106 { .mask = CHG_IRQ_CHG_I, },
107 { .mask = CHG_IRQ_CHGIN_I, },
108};
109
110static const struct regmap_irq_chip max77693_charger_irq_chip = {
111 .name = "max77693-charger",
112 .status_base = MAX77693_CHG_REG_CHG_INT,
113 .mask_base = MAX77693_CHG_REG_CHG_INT_MASK,
114 .mask_invert = false,
115 .num_regs = 1,
116 .irqs = max77693_charger_irqs,
117 .num_irqs = ARRAY_SIZE(max77693_charger_irqs),
118};
119
Krzysztof Kozlowski75ad1322013-12-20 10:35:08 +0100120static const struct regmap_config max77693_regmap_muic_config = {
121 .reg_bits = 8,
122 .val_bits = 8,
123 .max_register = MAX77693_MUIC_REG_END,
124};
125
Robert Baldyga342d6692014-05-21 08:52:48 +0200126static const struct regmap_irq max77693_muic_irqs[] = {
127 { .reg_offset = 0, .mask = MUIC_IRQ_INT1_ADC, },
128 { .reg_offset = 0, .mask = MUIC_IRQ_INT1_ADC_LOW, },
129 { .reg_offset = 0, .mask = MUIC_IRQ_INT1_ADC_ERR, },
130 { .reg_offset = 0, .mask = MUIC_IRQ_INT1_ADC1K, },
131
132 { .reg_offset = 1, .mask = MUIC_IRQ_INT2_CHGTYP, },
133 { .reg_offset = 1, .mask = MUIC_IRQ_INT2_CHGDETREUN, },
134 { .reg_offset = 1, .mask = MUIC_IRQ_INT2_DCDTMR, },
135 { .reg_offset = 1, .mask = MUIC_IRQ_INT2_DXOVP, },
136 { .reg_offset = 1, .mask = MUIC_IRQ_INT2_VBVOLT, },
137 { .reg_offset = 1, .mask = MUIC_IRQ_INT2_VIDRM, },
138
139 { .reg_offset = 2, .mask = MUIC_IRQ_INT3_EOC, },
140 { .reg_offset = 2, .mask = MUIC_IRQ_INT3_CGMBC, },
141 { .reg_offset = 2, .mask = MUIC_IRQ_INT3_OVP, },
142 { .reg_offset = 2, .mask = MUIC_IRQ_INT3_MBCCHG_ERR, },
143 { .reg_offset = 2, .mask = MUIC_IRQ_INT3_CHG_ENABLED, },
144 { .reg_offset = 2, .mask = MUIC_IRQ_INT3_BAT_DET, },
145};
146
147static const struct regmap_irq_chip max77693_muic_irq_chip = {
148 .name = "max77693-muic",
149 .status_base = MAX77693_MUIC_REG_INT1,
150 .mask_base = MAX77693_MUIC_REG_INTMASK1,
151 .mask_invert = true,
152 .num_regs = 3,
153 .irqs = max77693_muic_irqs,
154 .num_irqs = ARRAY_SIZE(max77693_muic_irqs),
155};
156
Jaewon Kimefbf4922014-09-18 01:40:24 +0900157static const struct regmap_config max77693_regmap_haptic_config = {
158 .reg_bits = 8,
159 .val_bits = 8,
160 .max_register = MAX77693_HAPTIC_REG_END,
161};
162
Chanwoo Choi83871c02012-05-14 22:50:39 +0200163static int max77693_i2c_probe(struct i2c_client *i2c,
164 const struct i2c_device_id *id)
165{
166 struct max77693_dev *max77693;
Robert Baldygad0540f92014-05-21 08:52:47 +0200167 unsigned int reg_data;
Chanwoo Choi83871c02012-05-14 22:50:39 +0200168 int ret = 0;
169
170 max77693 = devm_kzalloc(&i2c->dev,
171 sizeof(struct max77693_dev), GFP_KERNEL);
172 if (max77693 == NULL)
173 return -ENOMEM;
174
Chanwoo Choi83871c02012-05-14 22:50:39 +0200175 i2c_set_clientdata(i2c, max77693);
176 max77693->dev = &i2c->dev;
177 max77693->i2c = i2c;
178 max77693->irq = i2c->irq;
179 max77693->type = id->driver_data;
180
Axel Lin2429d862012-12-25 00:28:36 +0800181 max77693->regmap = devm_regmap_init_i2c(i2c, &max77693_regmap_config);
182 if (IS_ERR(max77693->regmap)) {
183 ret = PTR_ERR(max77693->regmap);
184 dev_err(max77693->dev, "failed to allocate register map: %d\n",
185 ret);
186 return ret;
187 }
Chanwoo Choi83871c02012-05-14 22:50:39 +0200188
Robert Baldygad0540f92014-05-21 08:52:47 +0200189 ret = regmap_read(max77693->regmap, MAX77693_PMIC_REG_PMIC_ID2,
Axel Lin2429d862012-12-25 00:28:36 +0800190 &reg_data);
191 if (ret < 0) {
Chanwoo Choi83871c02012-05-14 22:50:39 +0200192 dev_err(max77693->dev, "device not found on this channel\n");
Axel Lin2429d862012-12-25 00:28:36 +0800193 return ret;
Chanwoo Choi83871c02012-05-14 22:50:39 +0200194 } else
195 dev_info(max77693->dev, "device ID: 0x%x\n", reg_data);
196
Krzysztof Kozlowski61b305c2015-07-15 21:59:50 +0900197 max77693->i2c_muic = i2c_new_dummy(i2c->adapter, I2C_ADDR_MUIC);
198 if (!max77693->i2c_muic) {
Krzysztof Kozlowskiad09dd62014-02-11 11:03:32 +0100199 dev_err(max77693->dev, "Failed to allocate I2C device for MUIC\n");
200 return -ENODEV;
201 }
Krzysztof Kozlowski61b305c2015-07-15 21:59:50 +0900202 i2c_set_clientdata(max77693->i2c_muic, max77693);
Chanwoo Choi83871c02012-05-14 22:50:39 +0200203
Krzysztof Kozlowski61b305c2015-07-15 21:59:50 +0900204 max77693->i2c_haptic = i2c_new_dummy(i2c->adapter, I2C_ADDR_HAPTIC);
205 if (!max77693->i2c_haptic) {
Krzysztof Kozlowskiad09dd62014-02-11 11:03:32 +0100206 dev_err(max77693->dev, "Failed to allocate I2C device for Haptic\n");
207 ret = -ENODEV;
208 goto err_i2c_haptic;
209 }
Krzysztof Kozlowski61b305c2015-07-15 21:59:50 +0900210 i2c_set_clientdata(max77693->i2c_haptic, max77693);
Chanwoo Choi83871c02012-05-14 22:50:39 +0200211
Krzysztof Kozlowski61b305c2015-07-15 21:59:50 +0900212 max77693->regmap_haptic = devm_regmap_init_i2c(max77693->i2c_haptic,
Jaewon Kimefbf4922014-09-18 01:40:24 +0900213 &max77693_regmap_haptic_config);
214 if (IS_ERR(max77693->regmap_haptic)) {
215 ret = PTR_ERR(max77693->regmap_haptic);
216 dev_err(max77693->dev,
217 "failed to initialize haptic register map: %d\n", ret);
218 goto err_regmap;
219 }
220
Chanwoo Choib186b122012-08-21 15:16:23 +0900221 /*
222 * Initialize register map for MUIC device because use regmap-muic
223 * instance of MUIC device when irq of max77693 is initialized
224 * before call max77693-muic probe() function.
225 */
Krzysztof Kozlowski61b305c2015-07-15 21:59:50 +0900226 max77693->regmap_muic = devm_regmap_init_i2c(max77693->i2c_muic,
Krzysztof Kozlowski75ad1322013-12-20 10:35:08 +0100227 &max77693_regmap_muic_config);
Chanwoo Choib186b122012-08-21 15:16:23 +0900228 if (IS_ERR(max77693->regmap_muic)) {
229 ret = PTR_ERR(max77693->regmap_muic);
230 dev_err(max77693->dev,
231 "failed to allocate register map: %d\n", ret);
Jaewon Kimefbf4922014-09-18 01:40:24 +0900232 goto err_regmap;
Chanwoo Choib186b122012-08-21 15:16:23 +0900233 }
234
Robert Baldyga342d6692014-05-21 08:52:48 +0200235 ret = regmap_add_irq_chip(max77693->regmap, max77693->irq,
236 IRQF_ONESHOT | IRQF_SHARED |
237 IRQF_TRIGGER_FALLING, 0,
238 &max77693_led_irq_chip,
239 &max77693->irq_data_led);
240 if (ret) {
241 dev_err(max77693->dev, "failed to add irq chip: %d\n", ret);
Jaewon Kimefbf4922014-09-18 01:40:24 +0900242 goto err_regmap;
Robert Baldyga342d6692014-05-21 08:52:48 +0200243 }
244
245 ret = regmap_add_irq_chip(max77693->regmap, max77693->irq,
246 IRQF_ONESHOT | IRQF_SHARED |
247 IRQF_TRIGGER_FALLING, 0,
248 &max77693_topsys_irq_chip,
249 &max77693->irq_data_topsys);
250 if (ret) {
251 dev_err(max77693->dev, "failed to add irq chip: %d\n", ret);
252 goto err_irq_topsys;
253 }
254
255 ret = regmap_add_irq_chip(max77693->regmap, max77693->irq,
256 IRQF_ONESHOT | IRQF_SHARED |
257 IRQF_TRIGGER_FALLING, 0,
258 &max77693_charger_irq_chip,
Krzysztof Kozlowski61b305c2015-07-15 21:59:50 +0900259 &max77693->irq_data_chg);
Robert Baldyga342d6692014-05-21 08:52:48 +0200260 if (ret) {
261 dev_err(max77693->dev, "failed to add irq chip: %d\n", ret);
262 goto err_irq_charger;
263 }
264
Krzysztof Kozlowski43fc9392014-10-10 10:22:01 +0200265 ret = regmap_add_irq_chip(max77693->regmap_muic, max77693->irq,
Robert Baldyga342d6692014-05-21 08:52:48 +0200266 IRQF_ONESHOT | IRQF_SHARED |
267 IRQF_TRIGGER_FALLING, 0,
268 &max77693_muic_irq_chip,
269 &max77693->irq_data_muic);
270 if (ret) {
271 dev_err(max77693->dev, "failed to add irq chip: %d\n", ret);
272 goto err_irq_muic;
273 }
Chanwoo Choi6592ebb2012-05-14 22:54:20 +0200274
Krzysztof Kozlowskic0acb812014-10-10 12:48:35 +0200275 /* Unmask interrupts from all blocks in interrupt source register */
276 ret = regmap_update_bits(max77693->regmap,
277 MAX77693_PMIC_REG_INTSRC_MASK,
278 SRC_IRQ_ALL, (unsigned int)~SRC_IRQ_ALL);
279 if (ret < 0) {
280 dev_err(max77693->dev,
281 "Could not unmask interrupts in INTSRC: %d\n",
282 ret);
283 goto err_intsrc;
284 }
285
Chanwoo Choi83871c02012-05-14 22:50:39 +0200286 pm_runtime_set_active(max77693->dev);
287
288 ret = mfd_add_devices(max77693->dev, -1, max77693_devs,
Mark Brown0848c942012-09-11 15:16:36 +0800289 ARRAY_SIZE(max77693_devs), NULL, 0, NULL);
Chanwoo Choi83871c02012-05-14 22:50:39 +0200290 if (ret < 0)
291 goto err_mfd;
292
293 return ret;
294
295err_mfd:
Robert Baldyga342d6692014-05-21 08:52:48 +0200296 mfd_remove_devices(max77693->dev);
Krzysztof Kozlowskic0acb812014-10-10 12:48:35 +0200297err_intsrc:
Robert Baldyga342d6692014-05-21 08:52:48 +0200298 regmap_del_irq_chip(max77693->irq, max77693->irq_data_muic);
299err_irq_muic:
Krzysztof Kozlowski61b305c2015-07-15 21:59:50 +0900300 regmap_del_irq_chip(max77693->irq, max77693->irq_data_chg);
Robert Baldyga342d6692014-05-21 08:52:48 +0200301err_irq_charger:
302 regmap_del_irq_chip(max77693->irq, max77693->irq_data_topsys);
303err_irq_topsys:
304 regmap_del_irq_chip(max77693->irq, max77693->irq_data_led);
Jaewon Kimefbf4922014-09-18 01:40:24 +0900305err_regmap:
Krzysztof Kozlowski61b305c2015-07-15 21:59:50 +0900306 i2c_unregister_device(max77693->i2c_haptic);
Krzysztof Kozlowskiad09dd62014-02-11 11:03:32 +0100307err_i2c_haptic:
Krzysztof Kozlowski61b305c2015-07-15 21:59:50 +0900308 i2c_unregister_device(max77693->i2c_muic);
Chanwoo Choi83871c02012-05-14 22:50:39 +0200309 return ret;
310}
311
312static int max77693_i2c_remove(struct i2c_client *i2c)
313{
314 struct max77693_dev *max77693 = i2c_get_clientdata(i2c);
315
316 mfd_remove_devices(max77693->dev);
Robert Baldyga342d6692014-05-21 08:52:48 +0200317
318 regmap_del_irq_chip(max77693->irq, max77693->irq_data_muic);
Krzysztof Kozlowski61b305c2015-07-15 21:59:50 +0900319 regmap_del_irq_chip(max77693->irq, max77693->irq_data_chg);
Robert Baldyga342d6692014-05-21 08:52:48 +0200320 regmap_del_irq_chip(max77693->irq, max77693->irq_data_topsys);
321 regmap_del_irq_chip(max77693->irq, max77693->irq_data_led);
322
Krzysztof Kozlowski61b305c2015-07-15 21:59:50 +0900323 i2c_unregister_device(max77693->i2c_muic);
324 i2c_unregister_device(max77693->i2c_haptic);
Chanwoo Choi83871c02012-05-14 22:50:39 +0200325
326 return 0;
327}
328
329static const struct i2c_device_id max77693_i2c_id[] = {
330 { "max77693", TYPE_MAX77693 },
331 { }
332};
333MODULE_DEVICE_TABLE(i2c, max77693_i2c_id);
334
Chanwoo Choi6592ebb2012-05-14 22:54:20 +0200335static int max77693_suspend(struct device *dev)
336{
337 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
338 struct max77693_dev *max77693 = i2c_get_clientdata(i2c);
339
Robert Baldyga342d6692014-05-21 08:52:48 +0200340 if (device_may_wakeup(dev)) {
341 enable_irq_wake(max77693->irq);
342 disable_irq(max77693->irq);
343 }
344
Chanwoo Choi6592ebb2012-05-14 22:54:20 +0200345 return 0;
346}
347
348static int max77693_resume(struct device *dev)
349{
350 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
351 struct max77693_dev *max77693 = i2c_get_clientdata(i2c);
352
Robert Baldyga342d6692014-05-21 08:52:48 +0200353 if (device_may_wakeup(dev)) {
354 disable_irq_wake(max77693->irq);
355 enable_irq(max77693->irq);
356 }
357
358 return 0;
Chanwoo Choi6592ebb2012-05-14 22:54:20 +0200359}
360
Mark Brown12477a32012-06-05 16:15:59 +0100361static const struct dev_pm_ops max77693_pm = {
Chanwoo Choi6592ebb2012-05-14 22:54:20 +0200362 .suspend = max77693_suspend,
363 .resume = max77693_resume,
364};
365
Andrzej Hajda46571852013-09-27 09:27:46 +0200366#ifdef CONFIG_OF
Krzysztof Kozlowski5ce9a552014-05-13 12:58:52 +0200367static const struct of_device_id max77693_dt_match[] = {
Andrzej Hajda46571852013-09-27 09:27:46 +0200368 { .compatible = "maxim,max77693" },
369 {},
370};
371#endif
372
Chanwoo Choi83871c02012-05-14 22:50:39 +0200373static struct i2c_driver max77693_i2c_driver = {
374 .driver = {
375 .name = "max77693",
Chanwoo Choi6592ebb2012-05-14 22:54:20 +0200376 .pm = &max77693_pm,
Andrzej Hajda46571852013-09-27 09:27:46 +0200377 .of_match_table = of_match_ptr(max77693_dt_match),
Chanwoo Choi83871c02012-05-14 22:50:39 +0200378 },
379 .probe = max77693_i2c_probe,
380 .remove = max77693_i2c_remove,
381 .id_table = max77693_i2c_id,
382};
383
384static int __init max77693_i2c_init(void)
385{
386 return i2c_add_driver(&max77693_i2c_driver);
387}
388/* init early so consumer devices can complete system boot */
389subsys_initcall(max77693_i2c_init);
390
391static void __exit max77693_i2c_exit(void)
392{
393 i2c_del_driver(&max77693_i2c_driver);
394}
395module_exit(max77693_i2c_exit);
396
397MODULE_DESCRIPTION("MAXIM 77693 multi-function core driver");
398MODULE_AUTHOR("SangYoung, Son <hello.son@samsung.com>");
399MODULE_LICENSE("GPL");