xref: /openbmc/linux/drivers/mfd/sec-core.c (revision 65aba1e0)
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/rtc.h>
29 #include <linux/mfd/samsung/s2mps11.h>
30 #include <linux/mfd/samsung/s2mps14.h>
31 #include <linux/mfd/samsung/s5m8763.h>
32 #include <linux/mfd/samsung/s5m8767.h>
33 #include <linux/regmap.h>
34 
35 static const struct mfd_cell s5m8751_devs[] = {
36 	{
37 		.name = "s5m8751-pmic",
38 	}, {
39 		.name = "s5m-charger",
40 	}, {
41 		.name = "s5m8751-codec",
42 	},
43 };
44 
45 static const struct mfd_cell s5m8763_devs[] = {
46 	{
47 		.name = "s5m8763-pmic",
48 	}, {
49 		.name = "s5m-rtc",
50 	}, {
51 		.name = "s5m-charger",
52 	},
53 };
54 
55 static const struct mfd_cell s5m8767_devs[] = {
56 	{
57 		.name = "s5m8767-pmic",
58 	}, {
59 		.name = "s5m-rtc",
60 	}, {
61 		.name = "s5m8767-clk",
62 	}
63 };
64 
65 static const struct mfd_cell s2mps11_devs[] = {
66 	{
67 		.name = "s2mps11-pmic",
68 	}, {
69 		.name = "s2mps11-clk",
70 	}
71 };
72 
73 static const struct mfd_cell s2mps14_devs[] = {
74 	{
75 		.name = "s2mps14-pmic",
76 	}, {
77 		.name = "s2mps14-rtc",
78 	}, {
79 		.name = "s2mps14-clk",
80 	}
81 };
82 
83 #ifdef CONFIG_OF
84 static struct of_device_id sec_dt_match[] = {
85 	{	.compatible = "samsung,s5m8767-pmic",
86 		.data = (void *)S5M8767X,
87 	},
88 	{	.compatible = "samsung,s2mps11-pmic",
89 		.data = (void *)S2MPS11X,
90 	},
91 	{	.compatible = "samsung,s2mps14-pmic",
92 		.data = (void *)S2MPS14X,
93 	},
94 	{},
95 };
96 #endif
97 
98 static bool s2mps11_volatile(struct device *dev, unsigned int reg)
99 {
100 	switch (reg) {
101 	case S2MPS11_REG_INT1M:
102 	case S2MPS11_REG_INT2M:
103 	case S2MPS11_REG_INT3M:
104 		return false;
105 	default:
106 		return true;
107 	}
108 }
109 
110 static bool s5m8763_volatile(struct device *dev, unsigned int reg)
111 {
112 	switch (reg) {
113 	case S5M8763_REG_IRQM1:
114 	case S5M8763_REG_IRQM2:
115 	case S5M8763_REG_IRQM3:
116 	case S5M8763_REG_IRQM4:
117 		return false;
118 	default:
119 		return true;
120 	}
121 }
122 
123 static const struct regmap_config sec_regmap_config = {
124 	.reg_bits = 8,
125 	.val_bits = 8,
126 };
127 
128 static const struct regmap_config s2mps11_regmap_config = {
129 	.reg_bits = 8,
130 	.val_bits = 8,
131 
132 	.max_register = S2MPS11_REG_L38CTRL,
133 	.volatile_reg = s2mps11_volatile,
134 	.cache_type = REGCACHE_FLAT,
135 };
136 
137 static const struct regmap_config s2mps14_regmap_config = {
138 	.reg_bits = 8,
139 	.val_bits = 8,
140 
141 	.max_register = S2MPS14_REG_LDODSCH3,
142 	.volatile_reg = s2mps11_volatile,
143 	.cache_type = REGCACHE_FLAT,
144 };
145 
146 static const struct regmap_config s5m8763_regmap_config = {
147 	.reg_bits = 8,
148 	.val_bits = 8,
149 
150 	.max_register = S5M8763_REG_LBCNFG2,
151 	.volatile_reg = s5m8763_volatile,
152 	.cache_type = REGCACHE_FLAT,
153 };
154 
155 static const struct regmap_config s5m8767_regmap_config = {
156 	.reg_bits = 8,
157 	.val_bits = 8,
158 
159 	.max_register = S5M8767_REG_LDO28CTRL,
160 	.volatile_reg = s2mps11_volatile,
161 	.cache_type = REGCACHE_FLAT,
162 };
163 
164 static const struct regmap_config s5m_rtc_regmap_config = {
165 	.reg_bits = 8,
166 	.val_bits = 8,
167 
168 	.max_register = SEC_RTC_REG_MAX,
169 };
170 
171 static const struct regmap_config s2mps14_rtc_regmap_config = {
172 	.reg_bits = 8,
173 	.val_bits = 8,
174 
175 	.max_register = S2MPS_RTC_REG_MAX,
176 };
177 
178 #ifdef CONFIG_OF
179 /*
180  * Only the common platform data elements for s5m8767 are parsed here from the
181  * device tree. Other sub-modules of s5m8767 such as pmic, rtc , charger and
182  * others have to parse their own platform data elements from device tree.
183  *
184  * The s5m8767 platform data structure is instantiated here and the drivers for
185  * the sub-modules need not instantiate another instance while parsing their
186  * platform data.
187  */
188 static struct sec_platform_data *sec_pmic_i2c_parse_dt_pdata(
189 					struct device *dev)
190 {
191 	struct sec_platform_data *pd;
192 
193 	pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
194 	if (!pd) {
195 		dev_err(dev, "could not allocate memory for pdata\n");
196 		return ERR_PTR(-ENOMEM);
197 	}
198 
199 	/*
200 	 * ToDo: the 'wakeup' member in the platform data is more of a linux
201 	 * specfic information. Hence, there is no binding for that yet and
202 	 * not parsed here.
203 	 */
204 
205 	return pd;
206 }
207 #else
208 static struct sec_platform_data *sec_pmic_i2c_parse_dt_pdata(
209 					struct device *dev)
210 {
211 	return NULL;
212 }
213 #endif
214 
215 static inline int sec_i2c_get_driver_data(struct i2c_client *i2c,
216 						const struct i2c_device_id *id)
217 {
218 #ifdef CONFIG_OF
219 	if (i2c->dev.of_node) {
220 		const struct of_device_id *match;
221 		match = of_match_node(sec_dt_match, i2c->dev.of_node);
222 		return (int)match->data;
223 	}
224 #endif
225 	return (int)id->driver_data;
226 }
227 
228 static int sec_pmic_probe(struct i2c_client *i2c,
229 			    const struct i2c_device_id *id)
230 {
231 	struct sec_platform_data *pdata = dev_get_platdata(&i2c->dev);
232 	const struct regmap_config *regmap, *regmap_rtc;
233 	struct sec_pmic_dev *sec_pmic;
234 	int ret;
235 
236 	sec_pmic = devm_kzalloc(&i2c->dev, sizeof(struct sec_pmic_dev),
237 				GFP_KERNEL);
238 	if (sec_pmic == NULL)
239 		return -ENOMEM;
240 
241 	i2c_set_clientdata(i2c, sec_pmic);
242 	sec_pmic->dev = &i2c->dev;
243 	sec_pmic->i2c = i2c;
244 	sec_pmic->irq = i2c->irq;
245 	sec_pmic->type = sec_i2c_get_driver_data(i2c, id);
246 
247 	if (sec_pmic->dev->of_node) {
248 		pdata = sec_pmic_i2c_parse_dt_pdata(sec_pmic->dev);
249 		if (IS_ERR(pdata)) {
250 			ret = PTR_ERR(pdata);
251 			return ret;
252 		}
253 		pdata->device_type = sec_pmic->type;
254 	}
255 	if (pdata) {
256 		sec_pmic->device_type = pdata->device_type;
257 		sec_pmic->ono = pdata->ono;
258 		sec_pmic->irq_base = pdata->irq_base;
259 		sec_pmic->wakeup = pdata->wakeup;
260 		sec_pmic->pdata = pdata;
261 	}
262 
263 	switch (sec_pmic->device_type) {
264 	case S2MPS11X:
265 		regmap = &s2mps11_regmap_config;
266 		/*
267 		 * The rtc-s5m driver does not support S2MPS11 and there
268 		 * is no mfd_cell for S2MPS11 RTC device.
269 		 * However we must pass something to devm_regmap_init_i2c()
270 		 * so use S5M-like regmap config even though it wouldn't work.
271 		 */
272 		regmap_rtc = &s5m_rtc_regmap_config;
273 		break;
274 	case S2MPS14X:
275 		regmap = &s2mps14_regmap_config;
276 		regmap_rtc = &s2mps14_rtc_regmap_config;
277 		break;
278 	case S5M8763X:
279 		regmap = &s5m8763_regmap_config;
280 		regmap_rtc = &s5m_rtc_regmap_config;
281 		break;
282 	case S5M8767X:
283 		regmap = &s5m8767_regmap_config;
284 		regmap_rtc = &s5m_rtc_regmap_config;
285 		break;
286 	default:
287 		regmap = &sec_regmap_config;
288 		regmap_rtc = &s5m_rtc_regmap_config;
289 		break;
290 	}
291 
292 	sec_pmic->regmap_pmic = devm_regmap_init_i2c(i2c, regmap);
293 	if (IS_ERR(sec_pmic->regmap_pmic)) {
294 		ret = PTR_ERR(sec_pmic->regmap_pmic);
295 		dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
296 			ret);
297 		return ret;
298 	}
299 
300 	sec_pmic->rtc = i2c_new_dummy(i2c->adapter, RTC_I2C_ADDR);
301 	if (!sec_pmic->rtc) {
302 		dev_err(&i2c->dev, "Failed to allocate I2C for RTC\n");
303 		return -ENODEV;
304 	}
305 	i2c_set_clientdata(sec_pmic->rtc, sec_pmic);
306 
307 	sec_pmic->regmap_rtc = devm_regmap_init_i2c(sec_pmic->rtc, regmap_rtc);
308 	if (IS_ERR(sec_pmic->regmap_rtc)) {
309 		ret = PTR_ERR(sec_pmic->regmap_rtc);
310 		dev_err(&i2c->dev, "Failed to allocate RTC register map: %d\n",
311 			ret);
312 		return ret;
313 	}
314 
315 	if (pdata && pdata->cfg_pmic_irq)
316 		pdata->cfg_pmic_irq();
317 
318 	sec_irq_init(sec_pmic);
319 
320 	pm_runtime_set_active(sec_pmic->dev);
321 
322 	switch (sec_pmic->device_type) {
323 	case S5M8751X:
324 		ret = mfd_add_devices(sec_pmic->dev, -1, s5m8751_devs,
325 				      ARRAY_SIZE(s5m8751_devs), NULL, 0, NULL);
326 		break;
327 	case S5M8763X:
328 		ret = mfd_add_devices(sec_pmic->dev, -1, s5m8763_devs,
329 				      ARRAY_SIZE(s5m8763_devs), NULL, 0, NULL);
330 		break;
331 	case S5M8767X:
332 		ret = mfd_add_devices(sec_pmic->dev, -1, s5m8767_devs,
333 				      ARRAY_SIZE(s5m8767_devs), NULL, 0, NULL);
334 		break;
335 	case S2MPS11X:
336 		ret = mfd_add_devices(sec_pmic->dev, -1, s2mps11_devs,
337 				      ARRAY_SIZE(s2mps11_devs), NULL, 0, NULL);
338 		break;
339 	case S2MPS14X:
340 		ret = mfd_add_devices(sec_pmic->dev, -1, s2mps14_devs,
341 				      ARRAY_SIZE(s2mps14_devs), NULL, 0, NULL);
342 		break;
343 	default:
344 		/* If this happens the probe function is problem */
345 		BUG();
346 	}
347 
348 	if (ret)
349 		goto err;
350 
351 	device_init_wakeup(sec_pmic->dev, sec_pmic->wakeup);
352 
353 	return ret;
354 
355 err:
356 	sec_irq_exit(sec_pmic);
357 	i2c_unregister_device(sec_pmic->rtc);
358 	return ret;
359 }
360 
361 static int sec_pmic_remove(struct i2c_client *i2c)
362 {
363 	struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
364 
365 	mfd_remove_devices(sec_pmic->dev);
366 	sec_irq_exit(sec_pmic);
367 	i2c_unregister_device(sec_pmic->rtc);
368 	return 0;
369 }
370 
371 #ifdef CONFIG_PM_SLEEP
372 static int sec_pmic_suspend(struct device *dev)
373 {
374 	struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
375 	struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
376 
377 	if (device_may_wakeup(dev)) {
378 		enable_irq_wake(sec_pmic->irq);
379 		/*
380 		 * PMIC IRQ must be disabled during suspend for RTC alarm
381 		 * to work properly.
382 		 * When device is woken up from suspend by RTC Alarm, an
383 		 * interrupt occurs before resuming I2C bus controller.
384 		 * The interrupt is handled by regmap_irq_thread which tries
385 		 * to read RTC registers. This read fails (I2C is still
386 		 * suspended) and RTC Alarm interrupt is disabled.
387 		 */
388 		disable_irq(sec_pmic->irq);
389 	}
390 
391 	return 0;
392 }
393 
394 static int sec_pmic_resume(struct device *dev)
395 {
396 	struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
397 	struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
398 
399 	if (device_may_wakeup(dev)) {
400 		disable_irq_wake(sec_pmic->irq);
401 		enable_irq(sec_pmic->irq);
402 	}
403 
404 	return 0;
405 }
406 #endif /* CONFIG_PM_SLEEP */
407 
408 static SIMPLE_DEV_PM_OPS(sec_pmic_pm_ops, sec_pmic_suspend, sec_pmic_resume);
409 
410 static const struct i2c_device_id sec_pmic_id[] = {
411 	{ "sec_pmic", 0 },
412 	{ }
413 };
414 MODULE_DEVICE_TABLE(i2c, sec_pmic_id);
415 
416 static struct i2c_driver sec_pmic_driver = {
417 	.driver = {
418 		   .name = "sec_pmic",
419 		   .owner = THIS_MODULE,
420 		   .pm = &sec_pmic_pm_ops,
421 		   .of_match_table = of_match_ptr(sec_dt_match),
422 	},
423 	.probe = sec_pmic_probe,
424 	.remove = sec_pmic_remove,
425 	.id_table = sec_pmic_id,
426 };
427 
428 static int __init sec_pmic_init(void)
429 {
430 	return i2c_add_driver(&sec_pmic_driver);
431 }
432 
433 subsys_initcall(sec_pmic_init);
434 
435 static void __exit sec_pmic_exit(void)
436 {
437 	i2c_del_driver(&sec_pmic_driver);
438 }
439 module_exit(sec_pmic_exit);
440 
441 MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
442 MODULE_DESCRIPTION("Core support for the S5M MFD");
443 MODULE_LICENSE("GPL");
444