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