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