Linux Audio

Check our new training course

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