xref: /openbmc/linux/drivers/mfd/max8997.c (revision 55eb9a6c)
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // max8997.c - mfd core driver for the Maxim 8966 and 8997
4 //
5 // Copyright (C) 2011 Samsung Electronics
6 // MyungJoo Ham <myungjoo.ham@samsung.com>
7 //
8 // This driver is based on max8998.c
9 
10 #include <linux/err.h>
11 #include <linux/slab.h>
12 #include <linux/i2c.h>
13 #include <linux/of.h>
14 #include <linux/of_device.h>
15 #include <linux/of_irq.h>
16 #include <linux/interrupt.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/init.h>
19 #include <linux/mutex.h>
20 #include <linux/mfd/core.h>
21 #include <linux/mfd/max8997.h>
22 #include <linux/mfd/max8997-private.h>
23 
24 #define I2C_ADDR_PMIC	(0xCC >> 1)
25 #define I2C_ADDR_MUIC	(0x4A >> 1)
26 #define I2C_ADDR_BATTERY	(0x6C >> 1)
27 #define I2C_ADDR_RTC	(0x0C >> 1)
28 #define I2C_ADDR_HAPTIC	(0x90 >> 1)
29 
30 static const struct mfd_cell max8997_devs[] = {
31 	{ .name = "max8997-pmic", },
32 	{ .name = "max8997-rtc", },
33 	{ .name = "max8997-battery", },
34 	{ .name = "max8997-haptic", },
35 	{ .name = "max8997-muic", },
36 	{ .name = "max8997-led", .id = 1 },
37 	{ .name = "max8997-led", .id = 2 },
38 };
39 
40 #ifdef CONFIG_OF
41 static const struct of_device_id max8997_pmic_dt_match[] = {
42 	{ .compatible = "maxim,max8997-pmic", .data = (void *)TYPE_MAX8997 },
43 	{},
44 };
45 #endif
46 
47 int max8997_read_reg(struct i2c_client *i2c, u8 reg, u8 *dest)
48 {
49 	struct max8997_dev *max8997 = i2c_get_clientdata(i2c);
50 	int ret;
51 
52 	mutex_lock(&max8997->iolock);
53 	ret = i2c_smbus_read_byte_data(i2c, reg);
54 	mutex_unlock(&max8997->iolock);
55 	if (ret < 0)
56 		return ret;
57 
58 	ret &= 0xff;
59 	*dest = ret;
60 	return 0;
61 }
62 EXPORT_SYMBOL_GPL(max8997_read_reg);
63 
64 int max8997_bulk_read(struct i2c_client *i2c, u8 reg, int count, u8 *buf)
65 {
66 	struct max8997_dev *max8997 = i2c_get_clientdata(i2c);
67 	int ret;
68 
69 	mutex_lock(&max8997->iolock);
70 	ret = i2c_smbus_read_i2c_block_data(i2c, reg, count, buf);
71 	mutex_unlock(&max8997->iolock);
72 	if (ret < 0)
73 		return ret;
74 
75 	return 0;
76 }
77 EXPORT_SYMBOL_GPL(max8997_bulk_read);
78 
79 int max8997_write_reg(struct i2c_client *i2c, u8 reg, u8 value)
80 {
81 	struct max8997_dev *max8997 = i2c_get_clientdata(i2c);
82 	int ret;
83 
84 	mutex_lock(&max8997->iolock);
85 	ret = i2c_smbus_write_byte_data(i2c, reg, value);
86 	mutex_unlock(&max8997->iolock);
87 	return ret;
88 }
89 EXPORT_SYMBOL_GPL(max8997_write_reg);
90 
91 int max8997_bulk_write(struct i2c_client *i2c, u8 reg, int count, u8 *buf)
92 {
93 	struct max8997_dev *max8997 = i2c_get_clientdata(i2c);
94 	int ret;
95 
96 	mutex_lock(&max8997->iolock);
97 	ret = i2c_smbus_write_i2c_block_data(i2c, reg, count, buf);
98 	mutex_unlock(&max8997->iolock);
99 	if (ret < 0)
100 		return ret;
101 
102 	return 0;
103 }
104 EXPORT_SYMBOL_GPL(max8997_bulk_write);
105 
106 int max8997_update_reg(struct i2c_client *i2c, u8 reg, u8 val, u8 mask)
107 {
108 	struct max8997_dev *max8997 = i2c_get_clientdata(i2c);
109 	int ret;
110 
111 	mutex_lock(&max8997->iolock);
112 	ret = i2c_smbus_read_byte_data(i2c, reg);
113 	if (ret >= 0) {
114 		u8 old_val = ret & 0xff;
115 		u8 new_val = (val & mask) | (old_val & (~mask));
116 		ret = i2c_smbus_write_byte_data(i2c, reg, new_val);
117 	}
118 	mutex_unlock(&max8997->iolock);
119 	return ret;
120 }
121 EXPORT_SYMBOL_GPL(max8997_update_reg);
122 
123 /*
124  * Only the common platform data elements for max8997 are parsed here from the
125  * device tree. Other sub-modules of max8997 such as pmic, rtc and others have
126  * to parse their own platform data elements from device tree.
127  *
128  * The max8997 platform data structure is instantiated here and the drivers for
129  * the sub-modules need not instantiate another instance while parsing their
130  * platform data.
131  */
132 static struct max8997_platform_data *max8997_i2c_parse_dt_pdata(
133 					struct device *dev)
134 {
135 	struct max8997_platform_data *pd;
136 
137 	pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
138 	if (!pd)
139 		return ERR_PTR(-ENOMEM);
140 
141 	pd->ono = irq_of_parse_and_map(dev->of_node, 1);
142 
143 	return pd;
144 }
145 
146 static inline unsigned long max8997_i2c_get_driver_data(struct i2c_client *i2c,
147 						const struct i2c_device_id *id)
148 {
149 	if (i2c->dev.of_node)
150 		return (unsigned long)of_device_get_match_data(&i2c->dev);
151 
152 	return id->driver_data;
153 }
154 
155 static int max8997_i2c_probe(struct i2c_client *i2c,
156 			    const struct i2c_device_id *id)
157 {
158 	struct max8997_dev *max8997;
159 	struct max8997_platform_data *pdata = dev_get_platdata(&i2c->dev);
160 	int ret = 0;
161 
162 	max8997 = devm_kzalloc(&i2c->dev, sizeof(struct max8997_dev),
163 				GFP_KERNEL);
164 	if (max8997 == NULL)
165 		return -ENOMEM;
166 
167 	i2c_set_clientdata(i2c, max8997);
168 	max8997->dev = &i2c->dev;
169 	max8997->i2c = i2c;
170 	max8997->type = max8997_i2c_get_driver_data(i2c, id);
171 	max8997->irq = i2c->irq;
172 
173 	if (IS_ENABLED(CONFIG_OF) && max8997->dev->of_node) {
174 		pdata = max8997_i2c_parse_dt_pdata(max8997->dev);
175 		if (IS_ERR(pdata))
176 			return PTR_ERR(pdata);
177 	}
178 
179 	if (!pdata)
180 		return ret;
181 
182 	max8997->pdata = pdata;
183 	max8997->ono = pdata->ono;
184 
185 	mutex_init(&max8997->iolock);
186 
187 	max8997->rtc = i2c_new_dummy_device(i2c->adapter, I2C_ADDR_RTC);
188 	if (IS_ERR(max8997->rtc)) {
189 		dev_err(max8997->dev, "Failed to allocate I2C device for RTC\n");
190 		return PTR_ERR(max8997->rtc);
191 	}
192 	i2c_set_clientdata(max8997->rtc, max8997);
193 
194 	max8997->haptic = i2c_new_dummy_device(i2c->adapter, I2C_ADDR_HAPTIC);
195 	if (IS_ERR(max8997->haptic)) {
196 		dev_err(max8997->dev, "Failed to allocate I2C device for Haptic\n");
197 		ret = PTR_ERR(max8997->haptic);
198 		goto err_i2c_haptic;
199 	}
200 	i2c_set_clientdata(max8997->haptic, max8997);
201 
202 	max8997->muic = i2c_new_dummy_device(i2c->adapter, I2C_ADDR_MUIC);
203 	if (IS_ERR(max8997->muic)) {
204 		dev_err(max8997->dev, "Failed to allocate I2C device for MUIC\n");
205 		ret = PTR_ERR(max8997->muic);
206 		goto err_i2c_muic;
207 	}
208 	i2c_set_clientdata(max8997->muic, max8997);
209 
210 	pm_runtime_set_active(max8997->dev);
211 
212 	max8997_irq_init(max8997);
213 
214 	ret = mfd_add_devices(max8997->dev, -1, max8997_devs,
215 			ARRAY_SIZE(max8997_devs),
216 			NULL, 0, NULL);
217 	if (ret < 0) {
218 		dev_err(max8997->dev, "failed to add MFD devices %d\n", ret);
219 		goto err_mfd;
220 	}
221 
222 	/*
223 	 * TODO: enable others (flash, muic, rtc, battery, ...) and
224 	 * check the return value
225 	 */
226 
227 	/* MAX8997 has a power button input. */
228 	device_init_wakeup(max8997->dev, true);
229 
230 	return ret;
231 
232 err_mfd:
233 	mfd_remove_devices(max8997->dev);
234 	i2c_unregister_device(max8997->muic);
235 err_i2c_muic:
236 	i2c_unregister_device(max8997->haptic);
237 err_i2c_haptic:
238 	i2c_unregister_device(max8997->rtc);
239 	return ret;
240 }
241 
242 static const struct i2c_device_id max8997_i2c_id[] = {
243 	{ "max8997", TYPE_MAX8997 },
244 	{ "max8966", TYPE_MAX8966 },
245 	{ }
246 };
247 
248 static u8 max8997_dumpaddr_pmic[] = {
249 	MAX8997_REG_INT1MSK,
250 	MAX8997_REG_INT2MSK,
251 	MAX8997_REG_INT3MSK,
252 	MAX8997_REG_INT4MSK,
253 	MAX8997_REG_MAINCON1,
254 	MAX8997_REG_MAINCON2,
255 	MAX8997_REG_BUCKRAMP,
256 	MAX8997_REG_BUCK1CTRL,
257 	MAX8997_REG_BUCK1DVS1,
258 	MAX8997_REG_BUCK1DVS2,
259 	MAX8997_REG_BUCK1DVS3,
260 	MAX8997_REG_BUCK1DVS4,
261 	MAX8997_REG_BUCK1DVS5,
262 	MAX8997_REG_BUCK1DVS6,
263 	MAX8997_REG_BUCK1DVS7,
264 	MAX8997_REG_BUCK1DVS8,
265 	MAX8997_REG_BUCK2CTRL,
266 	MAX8997_REG_BUCK2DVS1,
267 	MAX8997_REG_BUCK2DVS2,
268 	MAX8997_REG_BUCK2DVS3,
269 	MAX8997_REG_BUCK2DVS4,
270 	MAX8997_REG_BUCK2DVS5,
271 	MAX8997_REG_BUCK2DVS6,
272 	MAX8997_REG_BUCK2DVS7,
273 	MAX8997_REG_BUCK2DVS8,
274 	MAX8997_REG_BUCK3CTRL,
275 	MAX8997_REG_BUCK3DVS,
276 	MAX8997_REG_BUCK4CTRL,
277 	MAX8997_REG_BUCK4DVS,
278 	MAX8997_REG_BUCK5CTRL,
279 	MAX8997_REG_BUCK5DVS1,
280 	MAX8997_REG_BUCK5DVS2,
281 	MAX8997_REG_BUCK5DVS3,
282 	MAX8997_REG_BUCK5DVS4,
283 	MAX8997_REG_BUCK5DVS5,
284 	MAX8997_REG_BUCK5DVS6,
285 	MAX8997_REG_BUCK5DVS7,
286 	MAX8997_REG_BUCK5DVS8,
287 	MAX8997_REG_BUCK6CTRL,
288 	MAX8997_REG_BUCK6BPSKIPCTRL,
289 	MAX8997_REG_BUCK7CTRL,
290 	MAX8997_REG_BUCK7DVS,
291 	MAX8997_REG_LDO1CTRL,
292 	MAX8997_REG_LDO2CTRL,
293 	MAX8997_REG_LDO3CTRL,
294 	MAX8997_REG_LDO4CTRL,
295 	MAX8997_REG_LDO5CTRL,
296 	MAX8997_REG_LDO6CTRL,
297 	MAX8997_REG_LDO7CTRL,
298 	MAX8997_REG_LDO8CTRL,
299 	MAX8997_REG_LDO9CTRL,
300 	MAX8997_REG_LDO10CTRL,
301 	MAX8997_REG_LDO11CTRL,
302 	MAX8997_REG_LDO12CTRL,
303 	MAX8997_REG_LDO13CTRL,
304 	MAX8997_REG_LDO14CTRL,
305 	MAX8997_REG_LDO15CTRL,
306 	MAX8997_REG_LDO16CTRL,
307 	MAX8997_REG_LDO17CTRL,
308 	MAX8997_REG_LDO18CTRL,
309 	MAX8997_REG_LDO21CTRL,
310 	MAX8997_REG_MBCCTRL1,
311 	MAX8997_REG_MBCCTRL2,
312 	MAX8997_REG_MBCCTRL3,
313 	MAX8997_REG_MBCCTRL4,
314 	MAX8997_REG_MBCCTRL5,
315 	MAX8997_REG_MBCCTRL6,
316 	MAX8997_REG_OTPCGHCVS,
317 	MAX8997_REG_SAFEOUTCTRL,
318 	MAX8997_REG_LBCNFG1,
319 	MAX8997_REG_LBCNFG2,
320 	MAX8997_REG_BBCCTRL,
321 
322 	MAX8997_REG_FLASH1_CUR,
323 	MAX8997_REG_FLASH2_CUR,
324 	MAX8997_REG_MOVIE_CUR,
325 	MAX8997_REG_GSMB_CUR,
326 	MAX8997_REG_BOOST_CNTL,
327 	MAX8997_REG_LEN_CNTL,
328 	MAX8997_REG_FLASH_CNTL,
329 	MAX8997_REG_WDT_CNTL,
330 	MAX8997_REG_MAXFLASH1,
331 	MAX8997_REG_MAXFLASH2,
332 	MAX8997_REG_FLASHSTATUSMASK,
333 
334 	MAX8997_REG_GPIOCNTL1,
335 	MAX8997_REG_GPIOCNTL2,
336 	MAX8997_REG_GPIOCNTL3,
337 	MAX8997_REG_GPIOCNTL4,
338 	MAX8997_REG_GPIOCNTL5,
339 	MAX8997_REG_GPIOCNTL6,
340 	MAX8997_REG_GPIOCNTL7,
341 	MAX8997_REG_GPIOCNTL8,
342 	MAX8997_REG_GPIOCNTL9,
343 	MAX8997_REG_GPIOCNTL10,
344 	MAX8997_REG_GPIOCNTL11,
345 	MAX8997_REG_GPIOCNTL12,
346 
347 	MAX8997_REG_LDO1CONFIG,
348 	MAX8997_REG_LDO2CONFIG,
349 	MAX8997_REG_LDO3CONFIG,
350 	MAX8997_REG_LDO4CONFIG,
351 	MAX8997_REG_LDO5CONFIG,
352 	MAX8997_REG_LDO6CONFIG,
353 	MAX8997_REG_LDO7CONFIG,
354 	MAX8997_REG_LDO8CONFIG,
355 	MAX8997_REG_LDO9CONFIG,
356 	MAX8997_REG_LDO10CONFIG,
357 	MAX8997_REG_LDO11CONFIG,
358 	MAX8997_REG_LDO12CONFIG,
359 	MAX8997_REG_LDO13CONFIG,
360 	MAX8997_REG_LDO14CONFIG,
361 	MAX8997_REG_LDO15CONFIG,
362 	MAX8997_REG_LDO16CONFIG,
363 	MAX8997_REG_LDO17CONFIG,
364 	MAX8997_REG_LDO18CONFIG,
365 	MAX8997_REG_LDO21CONFIG,
366 
367 	MAX8997_REG_DVSOKTIMER1,
368 	MAX8997_REG_DVSOKTIMER2,
369 	MAX8997_REG_DVSOKTIMER4,
370 	MAX8997_REG_DVSOKTIMER5,
371 };
372 
373 static u8 max8997_dumpaddr_muic[] = {
374 	MAX8997_MUIC_REG_INTMASK1,
375 	MAX8997_MUIC_REG_INTMASK2,
376 	MAX8997_MUIC_REG_INTMASK3,
377 	MAX8997_MUIC_REG_CDETCTRL,
378 	MAX8997_MUIC_REG_CONTROL1,
379 	MAX8997_MUIC_REG_CONTROL2,
380 	MAX8997_MUIC_REG_CONTROL3,
381 };
382 
383 static u8 max8997_dumpaddr_haptic[] = {
384 	MAX8997_HAPTIC_REG_CONF1,
385 	MAX8997_HAPTIC_REG_CONF2,
386 	MAX8997_HAPTIC_REG_DRVCONF,
387 	MAX8997_HAPTIC_REG_CYCLECONF1,
388 	MAX8997_HAPTIC_REG_CYCLECONF2,
389 	MAX8997_HAPTIC_REG_SIGCONF1,
390 	MAX8997_HAPTIC_REG_SIGCONF2,
391 	MAX8997_HAPTIC_REG_SIGCONF3,
392 	MAX8997_HAPTIC_REG_SIGCONF4,
393 	MAX8997_HAPTIC_REG_SIGDC1,
394 	MAX8997_HAPTIC_REG_SIGDC2,
395 	MAX8997_HAPTIC_REG_SIGPWMDC1,
396 	MAX8997_HAPTIC_REG_SIGPWMDC2,
397 	MAX8997_HAPTIC_REG_SIGPWMDC3,
398 	MAX8997_HAPTIC_REG_SIGPWMDC4,
399 };
400 
401 static int max8997_freeze(struct device *dev)
402 {
403 	struct i2c_client *i2c = to_i2c_client(dev);
404 	struct max8997_dev *max8997 = i2c_get_clientdata(i2c);
405 	int i;
406 
407 	for (i = 0; i < ARRAY_SIZE(max8997_dumpaddr_pmic); i++)
408 		max8997_read_reg(i2c, max8997_dumpaddr_pmic[i],
409 				&max8997->reg_dump[i]);
410 
411 	for (i = 0; i < ARRAY_SIZE(max8997_dumpaddr_muic); i++)
412 		max8997_read_reg(i2c, max8997_dumpaddr_muic[i],
413 				&max8997->reg_dump[i + MAX8997_REG_PMIC_END]);
414 
415 	for (i = 0; i < ARRAY_SIZE(max8997_dumpaddr_haptic); i++)
416 		max8997_read_reg(i2c, max8997_dumpaddr_haptic[i],
417 				&max8997->reg_dump[i + MAX8997_REG_PMIC_END +
418 				MAX8997_MUIC_REG_END]);
419 
420 	return 0;
421 }
422 
423 static int max8997_restore(struct device *dev)
424 {
425 	struct i2c_client *i2c = to_i2c_client(dev);
426 	struct max8997_dev *max8997 = i2c_get_clientdata(i2c);
427 	int i;
428 
429 	for (i = 0; i < ARRAY_SIZE(max8997_dumpaddr_pmic); i++)
430 		max8997_write_reg(i2c, max8997_dumpaddr_pmic[i],
431 				max8997->reg_dump[i]);
432 
433 	for (i = 0; i < ARRAY_SIZE(max8997_dumpaddr_muic); i++)
434 		max8997_write_reg(i2c, max8997_dumpaddr_muic[i],
435 				max8997->reg_dump[i + MAX8997_REG_PMIC_END]);
436 
437 	for (i = 0; i < ARRAY_SIZE(max8997_dumpaddr_haptic); i++)
438 		max8997_write_reg(i2c, max8997_dumpaddr_haptic[i],
439 				max8997->reg_dump[i + MAX8997_REG_PMIC_END +
440 				MAX8997_MUIC_REG_END]);
441 
442 	return 0;
443 }
444 
445 static int max8997_suspend(struct device *dev)
446 {
447 	struct i2c_client *i2c = to_i2c_client(dev);
448 	struct max8997_dev *max8997 = i2c_get_clientdata(i2c);
449 
450 	disable_irq(max8997->irq);
451 	if (device_may_wakeup(dev))
452 		irq_set_irq_wake(max8997->irq, 1);
453 	return 0;
454 }
455 
456 static int max8997_resume(struct device *dev)
457 {
458 	struct i2c_client *i2c = to_i2c_client(dev);
459 	struct max8997_dev *max8997 = i2c_get_clientdata(i2c);
460 
461 	if (device_may_wakeup(dev))
462 		irq_set_irq_wake(max8997->irq, 0);
463 	enable_irq(max8997->irq);
464 	return max8997_irq_resume(max8997);
465 }
466 
467 static const struct dev_pm_ops max8997_pm = {
468 	.suspend = max8997_suspend,
469 	.resume = max8997_resume,
470 	.freeze = max8997_freeze,
471 	.restore = max8997_restore,
472 };
473 
474 static struct i2c_driver max8997_i2c_driver = {
475 	.driver = {
476 		   .name = "max8997",
477 		   .pm = &max8997_pm,
478 		   .suppress_bind_attrs = true,
479 		   .of_match_table = of_match_ptr(max8997_pmic_dt_match),
480 	},
481 	.probe = max8997_i2c_probe,
482 	.id_table = max8997_i2c_id,
483 };
484 
485 static int __init max8997_i2c_init(void)
486 {
487 	return i2c_add_driver(&max8997_i2c_driver);
488 }
489 /* init early so consumer devices can complete system boot */
490 subsys_initcall(max8997_i2c_init);
491