Revert "unicode: Don't special case ignorable code points"
[linux.git] / drivers / mfd / sec-core.c
blobcdfe738e1d76e63145e5888da1cecc122fbc3737
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd
4 // http://www.samsung.com
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>
28 static const struct mfd_cell s5m8767_devs[] = {
29 { .name = "s5m8767-pmic", },
30 { .name = "s5m-rtc", },
32 .name = "s5m8767-clk",
33 .of_compatible = "samsung,s5m8767-clk",
37 static const struct mfd_cell s2dos05_devs[] = {
38 { .name = "s2dos05-regulator", },
41 static const struct mfd_cell s2mps11_devs[] = {
42 { .name = "s2mps11-regulator", },
43 { .name = "s2mps14-rtc", },
45 .name = "s2mps11-clk",
46 .of_compatible = "samsung,s2mps11-clk",
50 static const struct mfd_cell s2mps13_devs[] = {
51 { .name = "s2mps13-regulator", },
52 { .name = "s2mps13-rtc", },
54 .name = "s2mps13-clk",
55 .of_compatible = "samsung,s2mps13-clk",
59 static const struct mfd_cell s2mps14_devs[] = {
60 { .name = "s2mps14-regulator", },
61 { .name = "s2mps14-rtc", },
63 .name = "s2mps14-clk",
64 .of_compatible = "samsung,s2mps14-clk",
68 static const struct mfd_cell s2mps15_devs[] = {
69 { .name = "s2mps15-regulator", },
70 { .name = "s2mps15-rtc", },
72 .name = "s2mps13-clk",
73 .of_compatible = "samsung,s2mps13-clk",
77 static const struct mfd_cell s2mpa01_devs[] = {
78 { .name = "s2mpa01-pmic", },
79 { .name = "s2mps14-rtc", },
82 static const struct mfd_cell s2mpu02_devs[] = {
83 { .name = "s2mpu02-regulator", },
86 static const struct of_device_id sec_dt_match[] = {
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 */
115 MODULE_DEVICE_TABLE(of, sec_dt_match);
117 static bool s2mpa01_volatile(struct device *dev, unsigned int reg)
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;
129 static bool s2mps11_volatile(struct device *dev, unsigned int reg)
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;
141 static bool s2mpu02_volatile(struct device *dev, unsigned int reg)
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;
153 static const struct regmap_config sec_regmap_config = {
154 .reg_bits = 8,
155 .val_bits = 8,
158 static const struct regmap_config s2mpa01_regmap_config = {
159 .reg_bits = 8,
160 .val_bits = 8,
162 .max_register = S2MPA01_REG_LDO_OVCB4,
163 .volatile_reg = s2mpa01_volatile,
164 .cache_type = REGCACHE_FLAT,
167 static const struct regmap_config s2mps11_regmap_config = {
168 .reg_bits = 8,
169 .val_bits = 8,
171 .max_register = S2MPS11_REG_L38CTRL,
172 .volatile_reg = s2mps11_volatile,
173 .cache_type = REGCACHE_FLAT,
176 static const struct regmap_config s2mps13_regmap_config = {
177 .reg_bits = 8,
178 .val_bits = 8,
180 .max_register = S2MPS13_REG_LDODSCH5,
181 .volatile_reg = s2mps11_volatile,
182 .cache_type = REGCACHE_FLAT,
185 static const struct regmap_config s2mps14_regmap_config = {
186 .reg_bits = 8,
187 .val_bits = 8,
189 .max_register = S2MPS14_REG_LDODSCH3,
190 .volatile_reg = s2mps11_volatile,
191 .cache_type = REGCACHE_FLAT,
194 static const struct regmap_config s2mps15_regmap_config = {
195 .reg_bits = 8,
196 .val_bits = 8,
198 .max_register = S2MPS15_REG_LDODSCH4,
199 .volatile_reg = s2mps11_volatile,
200 .cache_type = REGCACHE_FLAT,
203 static const struct regmap_config s2mpu02_regmap_config = {
204 .reg_bits = 8,
205 .val_bits = 8,
207 .max_register = S2MPU02_REG_DVSDATA,
208 .volatile_reg = s2mpu02_volatile,
209 .cache_type = REGCACHE_FLAT,
212 static const struct regmap_config s5m8767_regmap_config = {
213 .reg_bits = 8,
214 .val_bits = 8,
216 .max_register = S5M8767_REG_LDO28CTRL,
217 .volatile_reg = s2mps11_volatile,
218 .cache_type = REGCACHE_FLAT,
221 static void sec_pmic_dump_rev(struct sec_pmic_dev *sec_pmic)
223 unsigned int val;
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);
230 static void sec_pmic_configure(struct sec_pmic_dev *sec_pmic)
232 int err;
234 if (sec_pmic->device_type != S2MPS13X)
235 return;
237 if (sec_pmic->pdata->disable_wrstbi) {
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.
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);
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.
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.
262 static struct sec_platform_data *
263 sec_pmic_i2c_parse_dt_pdata(struct device *dev)
265 struct sec_platform_data *pd;
267 pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
268 if (!pd)
269 return ERR_PTR(-ENOMEM);
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;
278 static int sec_pmic_probe(struct i2c_client *i2c)
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;
286 sec_pmic = devm_kzalloc(&i2c->dev, sizeof(struct sec_pmic_dev),
287 GFP_KERNEL);
288 if (sec_pmic == NULL)
289 return -ENOMEM;
291 i2c_set_clientdata(i2c, sec_pmic);
292 sec_pmic->dev = &i2c->dev;
293 sec_pmic->i2c = i2c;
294 sec_pmic->irq = i2c->irq;
296 pdata = sec_pmic_i2c_parse_dt_pdata(sec_pmic->dev);
297 if (IS_ERR(pdata)) {
298 ret = PTR_ERR(pdata);
299 return ret;
302 sec_pmic->device_type = (unsigned long)of_device_get_match_data(sec_pmic->dev);
303 sec_pmic->pdata = pdata;
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;
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;
340 sec_irq_init(sec_pmic);
342 pm_runtime_set_active(sec_pmic->dev);
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;
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;
387 sec_pmic_configure(sec_pmic);
388 sec_pmic_dump_rev(sec_pmic);
390 return ret;
393 static void sec_pmic_shutdown(struct i2c_client *i2c)
395 struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
396 unsigned int reg, mask;
398 if (!sec_pmic->pdata->manual_poweroff)
399 return;
401 switch (sec_pmic->device_type) {
402 case S2MPS11X:
403 reg = S2MPS11_REG_CTRL1;
404 mask = S2MPS11_CTRL1_PWRHOLD_MASK;
405 break;
406 default:
408 * Currently only one board with S2MPS11 needs this, so just
409 * ignore the rest.
411 dev_warn(sec_pmic->dev,
412 "Unsupported device %lu for manual power off\n",
413 sec_pmic->device_type);
414 return;
417 regmap_update_bits(sec_pmic->regmap_pmic, reg, mask, 0);
420 static int sec_pmic_suspend(struct device *dev)
422 struct i2c_client *i2c = to_i2c_client(dev);
423 struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
425 if (device_may_wakeup(dev))
426 enable_irq_wake(sec_pmic->irq);
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.
436 disable_irq(sec_pmic->irq);
438 return 0;
441 static int sec_pmic_resume(struct device *dev)
443 struct i2c_client *i2c = to_i2c_client(dev);
444 struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
446 if (device_may_wakeup(dev))
447 disable_irq_wake(sec_pmic->irq);
448 enable_irq(sec_pmic->irq);
450 return 0;
453 static DEFINE_SIMPLE_DEV_PM_OPS(sec_pmic_pm_ops,
454 sec_pmic_suspend, sec_pmic_resume);
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,
462 .probe = sec_pmic_probe,
463 .shutdown = sec_pmic_shutdown,
465 module_i2c_driver(sec_pmic_driver);
467 MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
468 MODULE_DESCRIPTION("Core support for the S5M MFD");
469 MODULE_LICENSE("GPL");