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