xref: /openbmc/linux/drivers/mfd/max77693.c (revision 3932b9ca)
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-flash", },
48 	{ .name = "max77693-muic", },
49 	{ .name = "max77693-haptic", },
50 };
51 
52 static const struct regmap_config max77693_regmap_config = {
53 	.reg_bits = 8,
54 	.val_bits = 8,
55 	.max_register = MAX77693_PMIC_REG_END,
56 };
57 
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,   },
64 };
65 
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,
70 	.mask_invert		= false,
71 	.num_regs		= 1,
72 	.irqs			= max77693_led_irqs,
73 	.num_irqs		= ARRAY_SIZE(max77693_led_irqs),
74 };
75 
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, },
80 };
81 
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,
86 	.mask_invert		= false,
87 	.num_regs		= 1,
88 	.irqs			= max77693_topsys_irqs,
89 	.num_irqs		= ARRAY_SIZE(max77693_topsys_irqs),
90 };
91 
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, },
98 };
99 
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,
105 	.num_regs		= 1,
106 	.irqs			= max77693_charger_irqs,
107 	.num_irqs		= ARRAY_SIZE(max77693_charger_irqs),
108 };
109 
110 static const struct regmap_config max77693_regmap_muic_config = {
111 	.reg_bits = 8,
112 	.val_bits = 8,
113 	.max_register = MAX77693_MUIC_REG_END,
114 };
115 
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,		},
121 
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,		},
128 
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,	},
135 };
136 
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,
141 	.mask_invert		= true,
142 	.num_regs		= 3,
143 	.irqs			= max77693_muic_irqs,
144 	.num_irqs		= ARRAY_SIZE(max77693_muic_irqs),
145 };
146 
147 static int max77693_i2c_probe(struct i2c_client *i2c,
148 			      const struct i2c_device_id *id)
149 {
150 	struct max77693_dev *max77693;
151 	unsigned int reg_data;
152 	int ret = 0;
153 
154 	max77693 = devm_kzalloc(&i2c->dev,
155 			sizeof(struct max77693_dev), GFP_KERNEL);
156 	if (max77693 == NULL)
157 		return -ENOMEM;
158 
159 	i2c_set_clientdata(i2c, max77693);
160 	max77693->dev = &i2c->dev;
161 	max77693->i2c = i2c;
162 	max77693->irq = i2c->irq;
163 	max77693->type = id->driver_data;
164 
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",
169 				ret);
170 		return ret;
171 	}
172 
173 	ret = regmap_read(max77693->regmap, MAX77693_PMIC_REG_PMIC_ID2,
174 				&reg_data);
175 	if (ret < 0) {
176 		dev_err(max77693->dev, "device not found on this channel\n");
177 		return ret;
178 	} else
179 		dev_info(max77693->dev, "device ID: 0x%x\n", reg_data);
180 
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");
184 		return -ENODEV;
185 	}
186 	i2c_set_clientdata(max77693->muic, max77693);
187 
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");
191 		ret = -ENODEV;
192 		goto err_i2c_haptic;
193 	}
194 	i2c_set_clientdata(max77693->haptic, max77693);
195 
196 	/*
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.
200 	 */
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;
208 	}
209 
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);
215 	if (ret) {
216 		dev_err(max77693->dev, "failed to add irq chip: %d\n", ret);
217 		goto err_regmap_muic;
218 	}
219 
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);
225 	if (ret) {
226 		dev_err(max77693->dev, "failed to add irq chip: %d\n", ret);
227 		goto err_irq_topsys;
228 	}
229 
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);
235 	if (ret) {
236 		dev_err(max77693->dev, "failed to add irq chip: %d\n", ret);
237 		goto err_irq_charger;
238 	}
239 
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);
245 	if (ret) {
246 		dev_err(max77693->dev, "failed to add irq chip: %d\n", ret);
247 		goto err_irq_muic;
248 	}
249 
250 	pm_runtime_set_active(max77693->dev);
251 
252 	ret = mfd_add_devices(max77693->dev, -1, max77693_devs,
253 			      ARRAY_SIZE(max77693_devs), NULL, 0, NULL);
254 	if (ret < 0)
255 		goto err_mfd;
256 
257 	return ret;
258 
259 err_mfd:
260 	mfd_remove_devices(max77693->dev);
261 	regmap_del_irq_chip(max77693->irq, max77693->irq_data_muic);
262 err_irq_muic:
263 	regmap_del_irq_chip(max77693->irq, max77693->irq_data_charger);
264 err_irq_charger:
265 	regmap_del_irq_chip(max77693->irq, max77693->irq_data_topsys);
266 err_irq_topsys:
267 	regmap_del_irq_chip(max77693->irq, max77693->irq_data_led);
268 err_regmap_muic:
269 	i2c_unregister_device(max77693->haptic);
270 err_i2c_haptic:
271 	i2c_unregister_device(max77693->muic);
272 	return ret;
273 }
274 
275 static int max77693_i2c_remove(struct i2c_client *i2c)
276 {
277 	struct max77693_dev *max77693 = i2c_get_clientdata(i2c);
278 
279 	mfd_remove_devices(max77693->dev);
280 
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);
285 
286 	i2c_unregister_device(max77693->muic);
287 	i2c_unregister_device(max77693->haptic);
288 
289 	return 0;
290 }
291 
292 static const struct i2c_device_id max77693_i2c_id[] = {
293 	{ "max77693", TYPE_MAX77693 },
294 	{ }
295 };
296 MODULE_DEVICE_TABLE(i2c, max77693_i2c_id);
297 
298 static int max77693_suspend(struct device *dev)
299 {
300 	struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
301 	struct max77693_dev *max77693 = i2c_get_clientdata(i2c);
302 
303 	if (device_may_wakeup(dev)) {
304 		enable_irq_wake(max77693->irq);
305 		disable_irq(max77693->irq);
306 	}
307 
308 	return 0;
309 }
310 
311 static int max77693_resume(struct device *dev)
312 {
313 	struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
314 	struct max77693_dev *max77693 = i2c_get_clientdata(i2c);
315 
316 	if (device_may_wakeup(dev)) {
317 		disable_irq_wake(max77693->irq);
318 		enable_irq(max77693->irq);
319 	}
320 
321 	return 0;
322 }
323 
324 static const struct dev_pm_ops max77693_pm = {
325 	.suspend = max77693_suspend,
326 	.resume = max77693_resume,
327 };
328 
329 #ifdef CONFIG_OF
330 static const struct of_device_id max77693_dt_match[] = {
331 	{ .compatible = "maxim,max77693" },
332 	{},
333 };
334 #endif
335 
336 static struct i2c_driver max77693_i2c_driver = {
337 	.driver = {
338 		   .name = "max77693",
339 		   .owner = THIS_MODULE,
340 		   .pm = &max77693_pm,
341 		   .of_match_table = of_match_ptr(max77693_dt_match),
342 	},
343 	.probe = max77693_i2c_probe,
344 	.remove = max77693_i2c_remove,
345 	.id_table = max77693_i2c_id,
346 };
347 
348 static int __init max77693_i2c_init(void)
349 {
350 	return i2c_add_driver(&max77693_i2c_driver);
351 }
352 /* init early so consumer devices can complete system boot */
353 subsys_initcall(max77693_i2c_init);
354 
355 static void __exit max77693_i2c_exit(void)
356 {
357 	i2c_del_driver(&max77693_i2c_driver);
358 }
359 module_exit(max77693_i2c_exit);
360 
361 MODULE_DESCRIPTION("MAXIM 77693 multi-function core driver");
362 MODULE_AUTHOR("SangYoung, Son <hello.son@samsung.com>");
363 MODULE_LICENSE("GPL");
364