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