Commit | Line | Data |
---|---|---|
d7d8d7a2 KK |
1 | // SPDX-License-Identifier: GPL-2.0+ |
2 | // | |
3 | // max77693.c - mfd core driver for the MAX 77693 | |
4 | // | |
5 | // Copyright (C) 2012 Samsung Electronics | |
6 | // SangYoung Son <hello.son@samsung.com> | |
7 | // | |
8 | // This program is not provided / owned by Maxim Integrated Products. | |
9 | // | |
10 | // This driver is based on max8997.c | |
83871c00 CC |
11 | |
12 | #include <linux/module.h> | |
13 | #include <linux/slab.h> | |
14 | #include <linux/i2c.h> | |
15 | #include <linux/err.h> | |
16 | #include <linux/interrupt.h> | |
71c7f93e | 17 | #include <linux/of.h> |
83871c00 CC |
18 | #include <linux/pm_runtime.h> |
19 | #include <linux/mutex.h> | |
20 | #include <linux/mfd/core.h> | |
21 | #include <linux/mfd/max77693.h> | |
61b305cd | 22 | #include <linux/mfd/max77693-common.h> |
83871c00 CC |
23 | #include <linux/mfd/max77693-private.h> |
24 | #include <linux/regulator/machine.h> | |
25 | #include <linux/regmap.h> | |
26 | ||
27 | #define I2C_ADDR_PMIC (0xCC >> 1) /* Charger, Flash LED */ | |
28 | #define I2C_ADDR_MUIC (0x4A >> 1) | |
29 | #define I2C_ADDR_HAPTIC (0x90 >> 1) | |
30 | ||
7c0517b1 | 31 | static const struct mfd_cell max77693_devs[] = { |
83871c00 | 32 | { .name = "max77693-pmic", }, |
efa3ca41 KK |
33 | { |
34 | .name = "max77693-charger", | |
35 | .of_compatible = "maxim,max77693-charger", | |
36 | }, | |
4589515a MP |
37 | { |
38 | .name = "max77693-muic", | |
39 | .of_compatible = "maxim,max77693-muic", | |
40 | }, | |
d1bafd78 JK |
41 | { |
42 | .name = "max77693-haptic", | |
43 | .of_compatible = "maxim,max77693-haptic", | |
44 | }, | |
a0bc6072 | 45 | { |
224995d7 JA |
46 | .name = "max77693-led", |
47 | .of_compatible = "maxim,max77693-led", | |
a0bc6072 | 48 | }, |
83871c00 CC |
49 | }; |
50 | ||
83871c00 CC |
51 | static const struct regmap_config max77693_regmap_config = { |
52 | .reg_bits = 8, | |
53 | .val_bits = 8, | |
54 | .max_register = MAX77693_PMIC_REG_END, | |
55 | }; | |
56 | ||
342d669c RB |
57 | static const struct regmap_irq max77693_led_irqs[] = { |
58 | { .mask = LED_IRQ_FLED2_OPEN, }, | |
59 | { .mask = LED_IRQ_FLED2_SHORT, }, | |
60 | { .mask = LED_IRQ_FLED1_OPEN, }, | |
61 | { .mask = LED_IRQ_FLED1_SHORT, }, | |
62 | { .mask = LED_IRQ_MAX_FLASH, }, | |
63 | }; | |
64 | ||
65 | static const struct regmap_irq_chip max77693_led_irq_chip = { | |
66 | .name = "max77693-led", | |
67 | .status_base = MAX77693_LED_REG_FLASH_INT, | |
68 | .mask_base = MAX77693_LED_REG_FLASH_INT_MASK, | |
69 | .mask_invert = false, | |
70 | .num_regs = 1, | |
71 | .irqs = max77693_led_irqs, | |
72 | .num_irqs = ARRAY_SIZE(max77693_led_irqs), | |
73 | }; | |
74 | ||
75 | static const struct regmap_irq max77693_topsys_irqs[] = { | |
76 | { .mask = TOPSYS_IRQ_T120C_INT, }, | |
77 | { .mask = TOPSYS_IRQ_T140C_INT, }, | |
78 | { .mask = TOPSYS_IRQ_LOWSYS_INT, }, | |
79 | }; | |
80 | ||
81 | static const struct regmap_irq_chip max77693_topsys_irq_chip = { | |
82 | .name = "max77693-topsys", | |
83 | .status_base = MAX77693_PMIC_REG_TOPSYS_INT, | |
84 | .mask_base = MAX77693_PMIC_REG_TOPSYS_INT_MASK, | |
85 | .mask_invert = false, | |
86 | .num_regs = 1, | |
87 | .irqs = max77693_topsys_irqs, | |
88 | .num_irqs = ARRAY_SIZE(max77693_topsys_irqs), | |
89 | }; | |
90 | ||
91 | static const struct regmap_irq max77693_charger_irqs[] = { | |
92 | { .mask = CHG_IRQ_BYP_I, }, | |
93 | { .mask = CHG_IRQ_THM_I, }, | |
94 | { .mask = CHG_IRQ_BAT_I, }, | |
95 | { .mask = CHG_IRQ_CHG_I, }, | |
96 | { .mask = CHG_IRQ_CHGIN_I, }, | |
97 | }; | |
98 | ||
99 | static const struct regmap_irq_chip max77693_charger_irq_chip = { | |
100 | .name = "max77693-charger", | |
101 | .status_base = MAX77693_CHG_REG_CHG_INT, | |
102 | .mask_base = MAX77693_CHG_REG_CHG_INT_MASK, | |
103 | .mask_invert = false, | |
104 | .num_regs = 1, | |
105 | .irqs = max77693_charger_irqs, | |
106 | .num_irqs = ARRAY_SIZE(max77693_charger_irqs), | |
107 | }; | |
108 | ||
75ad1327 KK |
109 | static const struct regmap_config max77693_regmap_muic_config = { |
110 | .reg_bits = 8, | |
111 | .val_bits = 8, | |
112 | .max_register = MAX77693_MUIC_REG_END, | |
113 | }; | |
114 | ||
342d669c RB |
115 | static const struct regmap_irq max77693_muic_irqs[] = { |
116 | { .reg_offset = 0, .mask = MUIC_IRQ_INT1_ADC, }, | |
117 | { .reg_offset = 0, .mask = MUIC_IRQ_INT1_ADC_LOW, }, | |
118 | { .reg_offset = 0, .mask = MUIC_IRQ_INT1_ADC_ERR, }, | |
119 | { .reg_offset = 0, .mask = MUIC_IRQ_INT1_ADC1K, }, | |
120 | ||
121 | { .reg_offset = 1, .mask = MUIC_IRQ_INT2_CHGTYP, }, | |
122 | { .reg_offset = 1, .mask = MUIC_IRQ_INT2_CHGDETREUN, }, | |
123 | { .reg_offset = 1, .mask = MUIC_IRQ_INT2_DCDTMR, }, | |
124 | { .reg_offset = 1, .mask = MUIC_IRQ_INT2_DXOVP, }, | |
125 | { .reg_offset = 1, .mask = MUIC_IRQ_INT2_VBVOLT, }, | |
126 | { .reg_offset = 1, .mask = MUIC_IRQ_INT2_VIDRM, }, | |
127 | ||
128 | { .reg_offset = 2, .mask = MUIC_IRQ_INT3_EOC, }, | |
129 | { .reg_offset = 2, .mask = MUIC_IRQ_INT3_CGMBC, }, | |
130 | { .reg_offset = 2, .mask = MUIC_IRQ_INT3_OVP, }, | |
131 | { .reg_offset = 2, .mask = MUIC_IRQ_INT3_MBCCHG_ERR, }, | |
132 | { .reg_offset = 2, .mask = MUIC_IRQ_INT3_CHG_ENABLED, }, | |
133 | { .reg_offset = 2, .mask = MUIC_IRQ_INT3_BAT_DET, }, | |
134 | }; | |
135 | ||
136 | static const struct regmap_irq_chip max77693_muic_irq_chip = { | |
137 | .name = "max77693-muic", | |
138 | .status_base = MAX77693_MUIC_REG_INT1, | |
139 | .mask_base = MAX77693_MUIC_REG_INTMASK1, | |
140 | .mask_invert = true, | |
141 | .num_regs = 3, | |
142 | .irqs = max77693_muic_irqs, | |
143 | .num_irqs = ARRAY_SIZE(max77693_muic_irqs), | |
144 | }; | |
145 | ||
efbf4922 JK |
146 | static const struct regmap_config max77693_regmap_haptic_config = { |
147 | .reg_bits = 8, | |
148 | .val_bits = 8, | |
149 | .max_register = MAX77693_HAPTIC_REG_END, | |
150 | }; | |
151 | ||
83871c00 CC |
152 | static int max77693_i2c_probe(struct i2c_client *i2c, |
153 | const struct i2c_device_id *id) | |
154 | { | |
155 | struct max77693_dev *max77693; | |
d0540f91 | 156 | unsigned int reg_data; |
83871c00 CC |
157 | int ret = 0; |
158 | ||
159 | max77693 = devm_kzalloc(&i2c->dev, | |
160 | sizeof(struct max77693_dev), GFP_KERNEL); | |
161 | if (max77693 == NULL) | |
162 | return -ENOMEM; | |
163 | ||
83871c00 CC |
164 | i2c_set_clientdata(i2c, max77693); |
165 | max77693->dev = &i2c->dev; | |
166 | max77693->i2c = i2c; | |
167 | max77693->irq = i2c->irq; | |
168 | max77693->type = id->driver_data; | |
169 | ||
2429d863 AL |
170 | max77693->regmap = devm_regmap_init_i2c(i2c, &max77693_regmap_config); |
171 | if (IS_ERR(max77693->regmap)) { | |
172 | ret = PTR_ERR(max77693->regmap); | |
173 | dev_err(max77693->dev, "failed to allocate register map: %d\n", | |
174 | ret); | |
175 | return ret; | |
176 | } | |
83871c00 | 177 | |
d0540f91 | 178 | ret = regmap_read(max77693->regmap, MAX77693_PMIC_REG_PMIC_ID2, |
2429d863 AL |
179 | ®_data); |
180 | if (ret < 0) { | |
83871c00 | 181 | dev_err(max77693->dev, "device not found on this channel\n"); |
2429d863 | 182 | return ret; |
83871c00 CC |
183 | } else |
184 | dev_info(max77693->dev, "device ID: 0x%x\n", reg_data); | |
185 | ||
ad28edcb WS |
186 | max77693->i2c_muic = i2c_new_dummy_device(i2c->adapter, I2C_ADDR_MUIC); |
187 | if (IS_ERR(max77693->i2c_muic)) { | |
ad09dd6a | 188 | dev_err(max77693->dev, "Failed to allocate I2C device for MUIC\n"); |
ad28edcb | 189 | return PTR_ERR(max77693->i2c_muic); |
ad09dd6a | 190 | } |
61b305cd | 191 | i2c_set_clientdata(max77693->i2c_muic, max77693); |
83871c00 | 192 | |
ad28edcb WS |
193 | max77693->i2c_haptic = i2c_new_dummy_device(i2c->adapter, I2C_ADDR_HAPTIC); |
194 | if (IS_ERR(max77693->i2c_haptic)) { | |
ad09dd6a | 195 | dev_err(max77693->dev, "Failed to allocate I2C device for Haptic\n"); |
ad28edcb | 196 | ret = PTR_ERR(max77693->i2c_haptic); |
ad09dd6a KK |
197 | goto err_i2c_haptic; |
198 | } | |
61b305cd | 199 | i2c_set_clientdata(max77693->i2c_haptic, max77693); |
83871c00 | 200 | |
61b305cd | 201 | max77693->regmap_haptic = devm_regmap_init_i2c(max77693->i2c_haptic, |
efbf4922 JK |
202 | &max77693_regmap_haptic_config); |
203 | if (IS_ERR(max77693->regmap_haptic)) { | |
204 | ret = PTR_ERR(max77693->regmap_haptic); | |
205 | dev_err(max77693->dev, | |
206 | "failed to initialize haptic register map: %d\n", ret); | |
207 | goto err_regmap; | |
208 | } | |
209 | ||
b186b124 CC |
210 | /* |
211 | * Initialize register map for MUIC device because use regmap-muic | |
212 | * instance of MUIC device when irq of max77693 is initialized | |
213 | * before call max77693-muic probe() function. | |
214 | */ | |
61b305cd | 215 | max77693->regmap_muic = devm_regmap_init_i2c(max77693->i2c_muic, |
75ad1327 | 216 | &max77693_regmap_muic_config); |
b186b124 CC |
217 | if (IS_ERR(max77693->regmap_muic)) { |
218 | ret = PTR_ERR(max77693->regmap_muic); | |
219 | dev_err(max77693->dev, | |
220 | "failed to allocate register map: %d\n", ret); | |
efbf4922 | 221 | goto err_regmap; |
b186b124 CC |
222 | } |
223 | ||
342d669c | 224 | ret = regmap_add_irq_chip(max77693->regmap, max77693->irq, |
f5f082eb | 225 | IRQF_ONESHOT | IRQF_SHARED, 0, |
342d669c RB |
226 | &max77693_led_irq_chip, |
227 | &max77693->irq_data_led); | |
228 | if (ret) { | |
229 | dev_err(max77693->dev, "failed to add irq chip: %d\n", ret); | |
efbf4922 | 230 | goto err_regmap; |
342d669c RB |
231 | } |
232 | ||
233 | ret = regmap_add_irq_chip(max77693->regmap, max77693->irq, | |
f5f082eb | 234 | IRQF_ONESHOT | IRQF_SHARED, 0, |
342d669c RB |
235 | &max77693_topsys_irq_chip, |
236 | &max77693->irq_data_topsys); | |
237 | if (ret) { | |
238 | dev_err(max77693->dev, "failed to add irq chip: %d\n", ret); | |
239 | goto err_irq_topsys; | |
240 | } | |
241 | ||
242 | ret = regmap_add_irq_chip(max77693->regmap, max77693->irq, | |
f5f082eb | 243 | IRQF_ONESHOT | IRQF_SHARED, 0, |
342d669c | 244 | &max77693_charger_irq_chip, |
61b305cd | 245 | &max77693->irq_data_chg); |
342d669c RB |
246 | if (ret) { |
247 | dev_err(max77693->dev, "failed to add irq chip: %d\n", ret); | |
248 | goto err_irq_charger; | |
249 | } | |
250 | ||
43fc9396 | 251 | ret = regmap_add_irq_chip(max77693->regmap_muic, max77693->irq, |
f5f082eb | 252 | IRQF_ONESHOT | IRQF_SHARED, 0, |
342d669c RB |
253 | &max77693_muic_irq_chip, |
254 | &max77693->irq_data_muic); | |
255 | if (ret) { | |
256 | dev_err(max77693->dev, "failed to add irq chip: %d\n", ret); | |
257 | goto err_irq_muic; | |
258 | } | |
6592ebb3 | 259 | |
c0acb814 KK |
260 | /* Unmask interrupts from all blocks in interrupt source register */ |
261 | ret = regmap_update_bits(max77693->regmap, | |
262 | MAX77693_PMIC_REG_INTSRC_MASK, | |
263 | SRC_IRQ_ALL, (unsigned int)~SRC_IRQ_ALL); | |
264 | if (ret < 0) { | |
265 | dev_err(max77693->dev, | |
266 | "Could not unmask interrupts in INTSRC: %d\n", | |
267 | ret); | |
268 | goto err_intsrc; | |
269 | } | |
270 | ||
83871c00 CC |
271 | pm_runtime_set_active(max77693->dev); |
272 | ||
273 | ret = mfd_add_devices(max77693->dev, -1, max77693_devs, | |
0848c94f | 274 | ARRAY_SIZE(max77693_devs), NULL, 0, NULL); |
83871c00 CC |
275 | if (ret < 0) |
276 | goto err_mfd; | |
277 | ||
278 | return ret; | |
279 | ||
280 | err_mfd: | |
342d669c | 281 | mfd_remove_devices(max77693->dev); |
c0acb814 | 282 | err_intsrc: |
342d669c RB |
283 | regmap_del_irq_chip(max77693->irq, max77693->irq_data_muic); |
284 | err_irq_muic: | |
61b305cd | 285 | regmap_del_irq_chip(max77693->irq, max77693->irq_data_chg); |
342d669c RB |
286 | err_irq_charger: |
287 | regmap_del_irq_chip(max77693->irq, max77693->irq_data_topsys); | |
288 | err_irq_topsys: | |
289 | regmap_del_irq_chip(max77693->irq, max77693->irq_data_led); | |
efbf4922 | 290 | err_regmap: |
61b305cd | 291 | i2c_unregister_device(max77693->i2c_haptic); |
ad09dd6a | 292 | err_i2c_haptic: |
61b305cd | 293 | i2c_unregister_device(max77693->i2c_muic); |
83871c00 CC |
294 | return ret; |
295 | } | |
296 | ||
297 | static int max77693_i2c_remove(struct i2c_client *i2c) | |
298 | { | |
299 | struct max77693_dev *max77693 = i2c_get_clientdata(i2c); | |
300 | ||
301 | mfd_remove_devices(max77693->dev); | |
342d669c RB |
302 | |
303 | regmap_del_irq_chip(max77693->irq, max77693->irq_data_muic); | |
61b305cd | 304 | regmap_del_irq_chip(max77693->irq, max77693->irq_data_chg); |
342d669c RB |
305 | regmap_del_irq_chip(max77693->irq, max77693->irq_data_topsys); |
306 | regmap_del_irq_chip(max77693->irq, max77693->irq_data_led); | |
307 | ||
61b305cd KK |
308 | i2c_unregister_device(max77693->i2c_muic); |
309 | i2c_unregister_device(max77693->i2c_haptic); | |
83871c00 CC |
310 | |
311 | return 0; | |
312 | } | |
313 | ||
314 | static const struct i2c_device_id max77693_i2c_id[] = { | |
315 | { "max77693", TYPE_MAX77693 }, | |
316 | { } | |
317 | }; | |
318 | MODULE_DEVICE_TABLE(i2c, max77693_i2c_id); | |
319 | ||
6592ebb3 CC |
320 | static int max77693_suspend(struct device *dev) |
321 | { | |
1b5420e1 | 322 | struct i2c_client *i2c = to_i2c_client(dev); |
6592ebb3 CC |
323 | struct max77693_dev *max77693 = i2c_get_clientdata(i2c); |
324 | ||
342d669c RB |
325 | if (device_may_wakeup(dev)) { |
326 | enable_irq_wake(max77693->irq); | |
327 | disable_irq(max77693->irq); | |
328 | } | |
329 | ||
6592ebb3 CC |
330 | return 0; |
331 | } | |
332 | ||
333 | static int max77693_resume(struct device *dev) | |
334 | { | |
1b5420e1 | 335 | struct i2c_client *i2c = to_i2c_client(dev); |
6592ebb3 CC |
336 | struct max77693_dev *max77693 = i2c_get_clientdata(i2c); |
337 | ||
342d669c RB |
338 | if (device_may_wakeup(dev)) { |
339 | disable_irq_wake(max77693->irq); | |
340 | enable_irq(max77693->irq); | |
341 | } | |
342 | ||
343 | return 0; | |
6592ebb3 CC |
344 | } |
345 | ||
12477a32 | 346 | static const struct dev_pm_ops max77693_pm = { |
6592ebb3 CC |
347 | .suspend = max77693_suspend, |
348 | .resume = max77693_resume, | |
349 | }; | |
350 | ||
4657185c | 351 | #ifdef CONFIG_OF |
5ce9a55b | 352 | static const struct of_device_id max77693_dt_match[] = { |
4657185c AH |
353 | { .compatible = "maxim,max77693" }, |
354 | {}, | |
355 | }; | |
073d4aca | 356 | MODULE_DEVICE_TABLE(of, max77693_dt_match); |
4657185c AH |
357 | #endif |
358 | ||
83871c00 CC |
359 | static struct i2c_driver max77693_i2c_driver = { |
360 | .driver = { | |
361 | .name = "max77693", | |
6592ebb3 | 362 | .pm = &max77693_pm, |
4657185c | 363 | .of_match_table = of_match_ptr(max77693_dt_match), |
83871c00 CC |
364 | }, |
365 | .probe = max77693_i2c_probe, | |
366 | .remove = max77693_i2c_remove, | |
367 | .id_table = max77693_i2c_id, | |
368 | }; | |
369 | ||
073d4aca | 370 | module_i2c_driver(max77693_i2c_driver); |
83871c00 CC |
371 | |
372 | MODULE_DESCRIPTION("MAXIM 77693 multi-function core driver"); | |
373 | MODULE_AUTHOR("SangYoung, Son <hello.son@samsung.com>"); | |
374 | MODULE_LICENSE("GPL"); |