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