xref: /openbmc/linux/drivers/mfd/sec-core.c (revision 2c684d89)
1 /*
2  * sec-core.c
3  *
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd
5  *              http://www.samsung.com
6  *
7  *  This program is free software; you can redistribute  it and/or modify it
8  *  under  the terms of  the GNU General  Public License as published by the
9  *  Free Software Foundation;  either version 2 of the  License, or (at your
10  *  option) any later version.
11  *
12  */
13 
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/init.h>
17 #include <linux/err.h>
18 #include <linux/slab.h>
19 #include <linux/i2c.h>
20 #include <linux/of.h>
21 #include <linux/of_irq.h>
22 #include <linux/interrupt.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/mutex.h>
25 #include <linux/mfd/core.h>
26 #include <linux/mfd/samsung/core.h>
27 #include <linux/mfd/samsung/irq.h>
28 #include <linux/mfd/samsung/s2mpa01.h>
29 #include <linux/mfd/samsung/s2mps11.h>
30 #include <linux/mfd/samsung/s2mps13.h>
31 #include <linux/mfd/samsung/s2mps14.h>
32 #include <linux/mfd/samsung/s2mpu02.h>
33 #include <linux/mfd/samsung/s5m8763.h>
34 #include <linux/mfd/samsung/s5m8767.h>
35 #include <linux/regmap.h>
36 
37 static const struct mfd_cell s5m8751_devs[] = {
38 	{
39 		.name = "s5m8751-pmic",
40 	}, {
41 		.name = "s5m-charger",
42 	}, {
43 		.name = "s5m8751-codec",
44 	},
45 };
46 
47 static const struct mfd_cell s5m8763_devs[] = {
48 	{
49 		.name = "s5m8763-pmic",
50 	}, {
51 		.name = "s5m-rtc",
52 	}, {
53 		.name = "s5m-charger",
54 	},
55 };
56 
57 static const struct mfd_cell s5m8767_devs[] = {
58 	{
59 		.name = "s5m8767-pmic",
60 	}, {
61 		.name = "s5m-rtc",
62 	}, {
63 		.name = "s5m8767-clk",
64 		.of_compatible = "samsung,s5m8767-clk",
65 	}
66 };
67 
68 static const struct mfd_cell s2mps11_devs[] = {
69 	{
70 		.name = "s2mps11-pmic",
71 	}, {
72 		.name = "s2mps14-rtc",
73 	}, {
74 		.name = "s2mps11-clk",
75 		.of_compatible = "samsung,s2mps11-clk",
76 	}
77 };
78 
79 static const struct mfd_cell s2mps13_devs[] = {
80 	{ .name = "s2mps13-pmic", },
81 	{ .name = "s2mps13-rtc", },
82 	{
83 		.name = "s2mps13-clk",
84 		.of_compatible = "samsung,s2mps13-clk",
85 	},
86 };
87 
88 static const struct mfd_cell s2mps14_devs[] = {
89 	{
90 		.name = "s2mps14-pmic",
91 	}, {
92 		.name = "s2mps14-rtc",
93 	}, {
94 		.name = "s2mps14-clk",
95 		.of_compatible = "samsung,s2mps14-clk",
96 	}
97 };
98 
99 static const struct mfd_cell s2mpa01_devs[] = {
100 	{
101 		.name = "s2mpa01-pmic",
102 	},
103 };
104 
105 static const struct mfd_cell s2mpu02_devs[] = {
106 	{
107 		.name = "s2mpu02-pmic",
108 	},
109 };
110 
111 #ifdef CONFIG_OF
112 static const struct of_device_id sec_dt_match[] = {
113 	{	.compatible = "samsung,s5m8767-pmic",
114 		.data = (void *)S5M8767X,
115 	}, {
116 		.compatible = "samsung,s2mps11-pmic",
117 		.data = (void *)S2MPS11X,
118 	}, {
119 		.compatible = "samsung,s2mps13-pmic",
120 		.data = (void *)S2MPS13X,
121 	}, {
122 		.compatible = "samsung,s2mps14-pmic",
123 		.data = (void *)S2MPS14X,
124 	}, {
125 		.compatible = "samsung,s2mpa01-pmic",
126 		.data = (void *)S2MPA01,
127 	}, {
128 		.compatible = "samsung,s2mpu02-pmic",
129 		.data = (void *)S2MPU02,
130 	}, {
131 		/* Sentinel */
132 	},
133 };
134 #endif
135 
136 static bool s2mpa01_volatile(struct device *dev, unsigned int reg)
137 {
138 	switch (reg) {
139 	case S2MPA01_REG_INT1M:
140 	case S2MPA01_REG_INT2M:
141 	case S2MPA01_REG_INT3M:
142 		return false;
143 	default:
144 		return true;
145 	}
146 }
147 
148 static bool s2mps11_volatile(struct device *dev, unsigned int reg)
149 {
150 	switch (reg) {
151 	case S2MPS11_REG_INT1M:
152 	case S2MPS11_REG_INT2M:
153 	case S2MPS11_REG_INT3M:
154 		return false;
155 	default:
156 		return true;
157 	}
158 }
159 
160 static bool s2mpu02_volatile(struct device *dev, unsigned int reg)
161 {
162 	switch (reg) {
163 	case S2MPU02_REG_INT1M:
164 	case S2MPU02_REG_INT2M:
165 	case S2MPU02_REG_INT3M:
166 		return false;
167 	default:
168 		return true;
169 	}
170 }
171 
172 static bool s5m8763_volatile(struct device *dev, unsigned int reg)
173 {
174 	switch (reg) {
175 	case S5M8763_REG_IRQM1:
176 	case S5M8763_REG_IRQM2:
177 	case S5M8763_REG_IRQM3:
178 	case S5M8763_REG_IRQM4:
179 		return false;
180 	default:
181 		return true;
182 	}
183 }
184 
185 static const struct regmap_config sec_regmap_config = {
186 	.reg_bits = 8,
187 	.val_bits = 8,
188 };
189 
190 static const struct regmap_config s2mpa01_regmap_config = {
191 	.reg_bits = 8,
192 	.val_bits = 8,
193 
194 	.max_register = S2MPA01_REG_LDO_OVCB4,
195 	.volatile_reg = s2mpa01_volatile,
196 	.cache_type = REGCACHE_FLAT,
197 };
198 
199 static const struct regmap_config s2mps11_regmap_config = {
200 	.reg_bits = 8,
201 	.val_bits = 8,
202 
203 	.max_register = S2MPS11_REG_L38CTRL,
204 	.volatile_reg = s2mps11_volatile,
205 	.cache_type = REGCACHE_FLAT,
206 };
207 
208 static const struct regmap_config s2mps13_regmap_config = {
209 	.reg_bits = 8,
210 	.val_bits = 8,
211 
212 	.max_register = S2MPS13_REG_LDODSCH5,
213 	.volatile_reg = s2mps11_volatile,
214 	.cache_type = REGCACHE_FLAT,
215 };
216 
217 static const struct regmap_config s2mps14_regmap_config = {
218 	.reg_bits = 8,
219 	.val_bits = 8,
220 
221 	.max_register = S2MPS14_REG_LDODSCH3,
222 	.volatile_reg = s2mps11_volatile,
223 	.cache_type = REGCACHE_FLAT,
224 };
225 
226 static const struct regmap_config s2mpu02_regmap_config = {
227 	.reg_bits = 8,
228 	.val_bits = 8,
229 
230 	.max_register = S2MPU02_REG_DVSDATA,
231 	.volatile_reg = s2mpu02_volatile,
232 	.cache_type = REGCACHE_FLAT,
233 };
234 
235 static const struct regmap_config s5m8763_regmap_config = {
236 	.reg_bits = 8,
237 	.val_bits = 8,
238 
239 	.max_register = S5M8763_REG_LBCNFG2,
240 	.volatile_reg = s5m8763_volatile,
241 	.cache_type = REGCACHE_FLAT,
242 };
243 
244 static const struct regmap_config s5m8767_regmap_config = {
245 	.reg_bits = 8,
246 	.val_bits = 8,
247 
248 	.max_register = S5M8767_REG_LDO28CTRL,
249 	.volatile_reg = s2mps11_volatile,
250 	.cache_type = REGCACHE_FLAT,
251 };
252 
253 static void sec_pmic_dump_rev(struct sec_pmic_dev *sec_pmic)
254 {
255 	unsigned int val;
256 
257 	/* For each device type, the REG_ID is always the first register */
258 	if (!regmap_read(sec_pmic->regmap_pmic, S2MPS11_REG_ID, &val))
259 		dev_dbg(sec_pmic->dev, "Revision: 0x%x\n", val);
260 }
261 
262 static void sec_pmic_configure(struct sec_pmic_dev *sec_pmic)
263 {
264 	int err;
265 
266 	if (sec_pmic->device_type != S2MPS13X)
267 		return;
268 
269 	if (sec_pmic->pdata->disable_wrstbi) {
270 		/*
271 		 * If WRSTBI pin is pulled down this feature must be disabled
272 		 * because each Suspend to RAM will trigger buck voltage reset
273 		 * to default values.
274 		 */
275 		err = regmap_update_bits(sec_pmic->regmap_pmic,
276 					 S2MPS13_REG_WRSTBI,
277 					 S2MPS13_REG_WRSTBI_MASK, 0x0);
278 		if (err)
279 			dev_warn(sec_pmic->dev,
280 				 "Cannot initialize WRSTBI config: %d\n",
281 				 err);
282 	}
283 }
284 
285 #ifdef CONFIG_OF
286 /*
287  * Only the common platform data elements for s5m8767 are parsed here from the
288  * device tree. Other sub-modules of s5m8767 such as pmic, rtc , charger and
289  * others have to parse their own platform data elements from device tree.
290  *
291  * The s5m8767 platform data structure is instantiated here and the drivers for
292  * the sub-modules need not instantiate another instance while parsing their
293  * platform data.
294  */
295 static struct sec_platform_data *sec_pmic_i2c_parse_dt_pdata(
296 					struct device *dev)
297 {
298 	struct sec_platform_data *pd;
299 
300 	pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
301 	if (!pd)
302 		return ERR_PTR(-ENOMEM);
303 
304 	/*
305 	 * ToDo: the 'wakeup' member in the platform data is more of a linux
306 	 * specfic information. Hence, there is no binding for that yet and
307 	 * not parsed here.
308 	 */
309 
310 	pd->manual_poweroff = of_property_read_bool(dev->of_node,
311 						"samsung,s2mps11-acokb-ground");
312 	pd->disable_wrstbi = of_property_read_bool(dev->of_node,
313 						"samsung,s2mps11-wrstbi-ground");
314 	return pd;
315 }
316 #else
317 static struct sec_platform_data *sec_pmic_i2c_parse_dt_pdata(
318 					struct device *dev)
319 {
320 	return NULL;
321 }
322 #endif
323 
324 static inline unsigned long sec_i2c_get_driver_data(struct i2c_client *i2c,
325 						const struct i2c_device_id *id)
326 {
327 #ifdef CONFIG_OF
328 	if (i2c->dev.of_node) {
329 		const struct of_device_id *match;
330 
331 		match = of_match_node(sec_dt_match, i2c->dev.of_node);
332 		return (unsigned long)match->data;
333 	}
334 #endif
335 	return id->driver_data;
336 }
337 
338 static int sec_pmic_probe(struct i2c_client *i2c,
339 			    const struct i2c_device_id *id)
340 {
341 	struct sec_platform_data *pdata = dev_get_platdata(&i2c->dev);
342 	const struct regmap_config *regmap;
343 	const struct mfd_cell *sec_devs;
344 	struct sec_pmic_dev *sec_pmic;
345 	unsigned long device_type;
346 	int ret, num_sec_devs;
347 
348 	sec_pmic = devm_kzalloc(&i2c->dev, sizeof(struct sec_pmic_dev),
349 				GFP_KERNEL);
350 	if (sec_pmic == NULL)
351 		return -ENOMEM;
352 
353 	i2c_set_clientdata(i2c, sec_pmic);
354 	sec_pmic->dev = &i2c->dev;
355 	sec_pmic->i2c = i2c;
356 	sec_pmic->irq = i2c->irq;
357 	device_type = sec_i2c_get_driver_data(i2c, id);
358 
359 	if (sec_pmic->dev->of_node) {
360 		pdata = sec_pmic_i2c_parse_dt_pdata(sec_pmic->dev);
361 		if (IS_ERR(pdata)) {
362 			ret = PTR_ERR(pdata);
363 			return ret;
364 		}
365 		pdata->device_type = device_type;
366 	}
367 	if (pdata) {
368 		sec_pmic->device_type = pdata->device_type;
369 		sec_pmic->irq_base = pdata->irq_base;
370 		sec_pmic->wakeup = pdata->wakeup;
371 		sec_pmic->pdata = pdata;
372 	}
373 
374 	switch (sec_pmic->device_type) {
375 	case S2MPA01:
376 		regmap = &s2mpa01_regmap_config;
377 		break;
378 	case S2MPS11X:
379 		regmap = &s2mps11_regmap_config;
380 		break;
381 	case S2MPS13X:
382 		regmap = &s2mps13_regmap_config;
383 		break;
384 	case S2MPS14X:
385 		regmap = &s2mps14_regmap_config;
386 		break;
387 	case S5M8763X:
388 		regmap = &s5m8763_regmap_config;
389 		break;
390 	case S5M8767X:
391 		regmap = &s5m8767_regmap_config;
392 		break;
393 	case S2MPU02:
394 		regmap = &s2mpu02_regmap_config;
395 		break;
396 	default:
397 		regmap = &sec_regmap_config;
398 		break;
399 	}
400 
401 	sec_pmic->regmap_pmic = devm_regmap_init_i2c(i2c, regmap);
402 	if (IS_ERR(sec_pmic->regmap_pmic)) {
403 		ret = PTR_ERR(sec_pmic->regmap_pmic);
404 		dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
405 			ret);
406 		return ret;
407 	}
408 
409 	if (pdata && pdata->cfg_pmic_irq)
410 		pdata->cfg_pmic_irq();
411 
412 	sec_irq_init(sec_pmic);
413 
414 	pm_runtime_set_active(sec_pmic->dev);
415 
416 	switch (sec_pmic->device_type) {
417 	case S5M8751X:
418 		sec_devs = s5m8751_devs;
419 		num_sec_devs = ARRAY_SIZE(s5m8751_devs);
420 		break;
421 	case S5M8763X:
422 		sec_devs = s5m8763_devs;
423 		num_sec_devs = ARRAY_SIZE(s5m8763_devs);
424 		break;
425 	case S5M8767X:
426 		sec_devs = s5m8767_devs;
427 		num_sec_devs = ARRAY_SIZE(s5m8767_devs);
428 		break;
429 	case S2MPA01:
430 		sec_devs = s2mpa01_devs;
431 		num_sec_devs = ARRAY_SIZE(s2mpa01_devs);
432 		break;
433 	case S2MPS11X:
434 		sec_devs = s2mps11_devs;
435 		num_sec_devs = ARRAY_SIZE(s2mps11_devs);
436 		break;
437 	case S2MPS13X:
438 		sec_devs = s2mps13_devs;
439 		num_sec_devs = ARRAY_SIZE(s2mps13_devs);
440 		break;
441 	case S2MPS14X:
442 		sec_devs = s2mps14_devs;
443 		num_sec_devs = ARRAY_SIZE(s2mps14_devs);
444 		break;
445 	case S2MPU02:
446 		sec_devs = s2mpu02_devs;
447 		num_sec_devs = ARRAY_SIZE(s2mpu02_devs);
448 		break;
449 	default:
450 		/* If this happens the probe function is problem */
451 		BUG();
452 	}
453 	ret = mfd_add_devices(sec_pmic->dev, -1, sec_devs, num_sec_devs, NULL,
454 			      0, NULL);
455 	if (ret)
456 		goto err_mfd;
457 
458 	device_init_wakeup(sec_pmic->dev, sec_pmic->wakeup);
459 	sec_pmic_configure(sec_pmic);
460 	sec_pmic_dump_rev(sec_pmic);
461 
462 	return ret;
463 
464 err_mfd:
465 	sec_irq_exit(sec_pmic);
466 	return ret;
467 }
468 
469 static int sec_pmic_remove(struct i2c_client *i2c)
470 {
471 	struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
472 
473 	mfd_remove_devices(sec_pmic->dev);
474 	sec_irq_exit(sec_pmic);
475 	return 0;
476 }
477 
478 static void sec_pmic_shutdown(struct i2c_client *i2c)
479 {
480 	struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
481 	unsigned int reg, mask;
482 
483 	if (!sec_pmic->pdata->manual_poweroff)
484 		return;
485 
486 	switch (sec_pmic->device_type) {
487 	case S2MPS11X:
488 		reg = S2MPS11_REG_CTRL1;
489 		mask = S2MPS11_CTRL1_PWRHOLD_MASK;
490 		break;
491 	default:
492 		/*
493 		 * Currently only one board with S2MPS11 needs this, so just
494 		 * ignore the rest.
495 		 */
496 		dev_warn(sec_pmic->dev,
497 			"Unsupported device %lu for manual power off\n",
498 			sec_pmic->device_type);
499 		return;
500 	}
501 
502 	regmap_update_bits(sec_pmic->regmap_pmic, reg, mask, 0);
503 }
504 
505 #ifdef CONFIG_PM_SLEEP
506 static int sec_pmic_suspend(struct device *dev)
507 {
508 	struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
509 	struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
510 
511 	if (device_may_wakeup(dev))
512 		enable_irq_wake(sec_pmic->irq);
513 	/*
514 	 * PMIC IRQ must be disabled during suspend for RTC alarm
515 	 * to work properly.
516 	 * When device is woken up from suspend, an
517 	 * interrupt occurs before resuming I2C bus controller.
518 	 * The interrupt is handled by regmap_irq_thread which tries
519 	 * to read RTC registers. This read fails (I2C is still
520 	 * suspended) and RTC Alarm interrupt is disabled.
521 	 */
522 	disable_irq(sec_pmic->irq);
523 
524 	return 0;
525 }
526 
527 static int sec_pmic_resume(struct device *dev)
528 {
529 	struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
530 	struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
531 
532 	if (device_may_wakeup(dev))
533 		disable_irq_wake(sec_pmic->irq);
534 	enable_irq(sec_pmic->irq);
535 
536 	return 0;
537 }
538 #endif /* CONFIG_PM_SLEEP */
539 
540 static SIMPLE_DEV_PM_OPS(sec_pmic_pm_ops, sec_pmic_suspend, sec_pmic_resume);
541 
542 static const struct i2c_device_id sec_pmic_id[] = {
543 	{ "sec_pmic", 0 },
544 	{ }
545 };
546 MODULE_DEVICE_TABLE(i2c, sec_pmic_id);
547 
548 static struct i2c_driver sec_pmic_driver = {
549 	.driver = {
550 		   .name = "sec_pmic",
551 		   .pm = &sec_pmic_pm_ops,
552 		   .of_match_table = of_match_ptr(sec_dt_match),
553 	},
554 	.probe = sec_pmic_probe,
555 	.remove = sec_pmic_remove,
556 	.shutdown = sec_pmic_shutdown,
557 	.id_table = sec_pmic_id,
558 };
559 
560 static int __init sec_pmic_init(void)
561 {
562 	return i2c_add_driver(&sec_pmic_driver);
563 }
564 
565 subsys_initcall(sec_pmic_init);
566 
567 static void __exit sec_pmic_exit(void)
568 {
569 	i2c_del_driver(&sec_pmic_driver);
570 }
571 module_exit(sec_pmic_exit);
572 
573 MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
574 MODULE_DESCRIPTION("Core support for the S5M MFD");
575 MODULE_LICENSE("GPL");
576