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