blob: 989076d6cb83415451fe0776c12506493993f12d [file] [log] [blame]
Sangbeom Kim0f5f7072011-12-23 17:28:08 +09001/*
Sangbeom Kim63063bf2012-07-11 21:06:55 +09002 * sec-core.c
Sangbeom Kim0f5f7072011-12-23 17:28:08 +09003 *
Sangbeom Kim63063bf2012-07-11 21:06:55 +09004 * Copyright (c) 2012 Samsung Electronics Co., Ltd
Sangbeom Kim0f5f7072011-12-23 17:28:08 +09005 * 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>
Sachin Kamat1c5a0992013-10-16 14:26:52 +053020#include <linux/of.h>
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -080021#include <linux/of_irq.h>
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090022#include <linux/interrupt.h>
23#include <linux/pm_runtime.h>
24#include <linux/mutex.h>
25#include <linux/mfd/core.h>
Sangbeom Kim54227bc2012-07-11 21:07:16 +090026#include <linux/mfd/samsung/core.h>
27#include <linux/mfd/samsung/irq.h>
Sachin Kamatce249912014-03-14 17:21:57 +053028#include <linux/mfd/samsung/s2mpa01.h>
Mark Brown25f311f2013-07-01 23:02:19 +010029#include <linux/mfd/samsung/s2mps11.h>
Chanwoo Choi76b98402014-11-18 17:59:40 +090030#include <linux/mfd/samsung/s2mps13.h>
Krzysztof Kozlowskidc691962014-02-28 11:41:44 +010031#include <linux/mfd/samsung/s2mps14.h>
Chanwoo Choi00e25732014-06-25 16:14:45 +090032#include <linux/mfd/samsung/s2mpu02.h>
Mark Brown25f311f2013-07-01 23:02:19 +010033#include <linux/mfd/samsung/s5m8763.h>
34#include <linux/mfd/samsung/s5m8767.h>
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090035#include <linux/regmap.h>
36
Geert Uytterhoeven5ac98552013-11-18 14:33:06 +010037static const struct mfd_cell s5m8751_devs[] = {
Sangbeom Kim88a1cfd2012-01-20 16:09:12 +090038 {
39 .name = "s5m8751-pmic",
40 }, {
41 .name = "s5m-charger",
42 }, {
43 .name = "s5m8751-codec",
44 },
45};
46
Geert Uytterhoeven5ac98552013-11-18 14:33:06 +010047static const struct mfd_cell s5m8763_devs[] = {
Sangbeom Kim88a1cfd2012-01-20 16:09:12 +090048 {
49 .name = "s5m8763-pmic",
50 }, {
51 .name = "s5m-rtc",
52 }, {
53 .name = "s5m-charger",
54 },
55};
56
Geert Uytterhoeven5ac98552013-11-18 14:33:06 +010057static const struct mfd_cell s5m8767_devs[] = {
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090058 {
59 .name = "s5m8767-pmic",
60 }, {
61 .name = "s5m-rtc",
Tushar Behera39fed002013-12-26 15:49:00 +053062 }, {
63 .name = "s5m8767-clk",
Krzysztof Kozlowski53c31b32014-03-17 10:19:17 +010064 .of_compatible = "samsung,s5m8767-clk",
Tushar Behera39fed002013-12-26 15:49:00 +053065 }
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090066};
67
Geert Uytterhoeven5ac98552013-11-18 14:33:06 +010068static const struct mfd_cell s2mps11_devs[] = {
Sangbeom Kim9b6d1342012-07-11 21:07:55 +090069 {
70 .name = "s2mps11-pmic",
Yadwinder Singh Brar3134bca2013-07-07 17:14:21 +053071 }, {
Krzysztof Kozlowskieea2e592015-03-17 13:36:17 +010072 .name = "s2mps14-rtc",
73 }, {
Yadwinder Singh Brar3134bca2013-07-07 17:14:21 +053074 .name = "s2mps11-clk",
Krzysztof Kozlowski53c31b32014-03-17 10:19:17 +010075 .of_compatible = "samsung,s2mps11-clk",
Yadwinder Singh Brar3134bca2013-07-07 17:14:21 +053076 }
Sangbeom Kim9b6d1342012-07-11 21:07:55 +090077};
78
Chanwoo Choi3bc2ee92014-11-18 17:59:39 +090079static const struct mfd_cell s2mps13_devs[] = {
80 { .name = "s2mps13-pmic", },
81 { .name = "s2mps13-rtc", },
82 {
83 .name = "s2mps13-clk",
84 .of_compatible = "samsung,s2mps13-clk",
85 },
86};
87
Krzysztof Kozlowskidc691962014-02-28 11:41:44 +010088static const struct mfd_cell s2mps14_devs[] = {
89 {
90 .name = "s2mps14-pmic",
91 }, {
92 .name = "s2mps14-rtc",
93 }, {
94 .name = "s2mps14-clk",
Krzysztof Kozlowski53c31b32014-03-17 10:19:17 +010095 .of_compatible = "samsung,s2mps14-clk",
Krzysztof Kozlowskidc691962014-02-28 11:41:44 +010096 }
97};
98
Sachin Kamatce249912014-03-14 17:21:57 +053099static const struct mfd_cell s2mpa01_devs[] = {
100 {
101 .name = "s2mpa01-pmic",
102 },
103};
104
Chanwoo Choi54e88272014-06-25 16:14:44 +0900105static const struct mfd_cell s2mpu02_devs[] = {
Chanwoo Choi54e88272014-06-25 16:14:44 +0900106 {
Krzysztof Kozlowski644a3742015-10-30 16:51:21 +0900107 .name = "s2mpu02-pmic",
108 },
Chanwoo Choi54e88272014-06-25 16:14:44 +0900109};
110
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800111#ifdef CONFIG_OF
Krzysztof Kozlowskidf20b7c2014-05-13 12:58:46 +0200112static const struct of_device_id sec_dt_match[] = {
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800113 { .compatible = "samsung,s5m8767-pmic",
114 .data = (void *)S5M8767X,
Sachin Kamatce249912014-03-14 17:21:57 +0530115 }, {
116 .compatible = "samsung,s2mps11-pmic",
Yadwinder Singh Braraa32aca2013-06-29 18:21:20 +0530117 .data = (void *)S2MPS11X,
Sachin Kamatce249912014-03-14 17:21:57 +0530118 }, {
Chanwoo Choi3bc2ee92014-11-18 17:59:39 +0900119 .compatible = "samsung,s2mps13-pmic",
120 .data = (void *)S2MPS13X,
121 }, {
Sachin Kamatce249912014-03-14 17:21:57 +0530122 .compatible = "samsung,s2mps14-pmic",
Krzysztof Kozlowskidc691962014-02-28 11:41:44 +0100123 .data = (void *)S2MPS14X,
Sachin Kamatce249912014-03-14 17:21:57 +0530124 }, {
125 .compatible = "samsung,s2mpa01-pmic",
126 .data = (void *)S2MPA01,
127 }, {
Chanwoo Choi54e88272014-06-25 16:14:44 +0900128 .compatible = "samsung,s2mpu02-pmic",
129 .data = (void *)S2MPU02,
130 }, {
Sachin Kamatce249912014-03-14 17:21:57 +0530131 /* Sentinel */
Krzysztof Kozlowskidc691962014-02-28 11:41:44 +0100132 },
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800133};
134#endif
135
Sachin Kamatce249912014-03-14 17:21:57 +0530136static bool s2mpa01_volatile(struct device *dev, unsigned int reg)
137{
138 switch (reg) {
139 case S2MPA01_REG_INT1M:
140 case S2MPA01_REG_INT2M:
141 case S2MPA01_REG_INT3M:
142 return false;
143 default:
144 return true;
145 }
146}
147
Mark Brown6b845ba2013-07-02 18:51:16 +0100148static bool s2mps11_volatile(struct device *dev, unsigned int reg)
149{
150 switch (reg) {
151 case S2MPS11_REG_INT1M:
152 case S2MPS11_REG_INT2M:
153 case S2MPS11_REG_INT3M:
154 return false;
155 default:
156 return true;
157 }
158}
159
Chanwoo Choi00e25732014-06-25 16:14:45 +0900160static bool s2mpu02_volatile(struct device *dev, unsigned int reg)
161{
162 switch (reg) {
163 case S2MPU02_REG_INT1M:
164 case S2MPU02_REG_INT2M:
165 case S2MPU02_REG_INT3M:
166 return false;
167 default:
168 return true;
169 }
170}
171
Mark Brown6b845ba2013-07-02 18:51:16 +0100172static bool s5m8763_volatile(struct device *dev, unsigned int reg)
173{
174 switch (reg) {
175 case S5M8763_REG_IRQM1:
176 case S5M8763_REG_IRQM2:
177 case S5M8763_REG_IRQM3:
178 case S5M8763_REG_IRQM4:
179 return false;
180 default:
181 return true;
182 }
183}
184
Krzysztof Kozlowskia30fffb2013-11-28 09:09:43 +0100185static const struct regmap_config sec_regmap_config = {
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900186 .reg_bits = 8,
187 .val_bits = 8,
188};
189
Sachin Kamatce249912014-03-14 17:21:57 +0530190static const struct regmap_config s2mpa01_regmap_config = {
191 .reg_bits = 8,
192 .val_bits = 8,
193
194 .max_register = S2MPA01_REG_LDO_OVCB4,
195 .volatile_reg = s2mpa01_volatile,
196 .cache_type = REGCACHE_FLAT,
197};
198
Krzysztof Kozlowskia30fffb2013-11-28 09:09:43 +0100199static const struct regmap_config s2mps11_regmap_config = {
Mark Brown25f311f2013-07-01 23:02:19 +0100200 .reg_bits = 8,
201 .val_bits = 8,
202
203 .max_register = S2MPS11_REG_L38CTRL,
Mark Brown6b845ba2013-07-02 18:51:16 +0100204 .volatile_reg = s2mps11_volatile,
205 .cache_type = REGCACHE_FLAT,
Mark Brown25f311f2013-07-01 23:02:19 +0100206};
207
Chanwoo Choi76b98402014-11-18 17:59:40 +0900208static const struct regmap_config s2mps13_regmap_config = {
209 .reg_bits = 8,
210 .val_bits = 8,
211
212 .max_register = S2MPS13_REG_LDODSCH5,
213 .volatile_reg = s2mps11_volatile,
214 .cache_type = REGCACHE_FLAT,
215};
216
Krzysztof Kozlowskidc691962014-02-28 11:41:44 +0100217static const struct regmap_config s2mps14_regmap_config = {
218 .reg_bits = 8,
219 .val_bits = 8,
220
221 .max_register = S2MPS14_REG_LDODSCH3,
222 .volatile_reg = s2mps11_volatile,
223 .cache_type = REGCACHE_FLAT,
224};
225
Chanwoo Choi00e25732014-06-25 16:14:45 +0900226static const struct regmap_config s2mpu02_regmap_config = {
227 .reg_bits = 8,
228 .val_bits = 8,
229
230 .max_register = S2MPU02_REG_DVSDATA,
231 .volatile_reg = s2mpu02_volatile,
232 .cache_type = REGCACHE_FLAT,
233};
234
Krzysztof Kozlowskia30fffb2013-11-28 09:09:43 +0100235static const struct regmap_config s5m8763_regmap_config = {
Mark Brown25f311f2013-07-01 23:02:19 +0100236 .reg_bits = 8,
237 .val_bits = 8,
238
239 .max_register = S5M8763_REG_LBCNFG2,
Mark Brown6b845ba2013-07-02 18:51:16 +0100240 .volatile_reg = s5m8763_volatile,
241 .cache_type = REGCACHE_FLAT,
Mark Brown25f311f2013-07-01 23:02:19 +0100242};
243
Krzysztof Kozlowskia30fffb2013-11-28 09:09:43 +0100244static const struct regmap_config s5m8767_regmap_config = {
Mark Brown25f311f2013-07-01 23:02:19 +0100245 .reg_bits = 8,
246 .val_bits = 8,
247
248 .max_register = S5M8767_REG_LDO28CTRL,
Mark Brown6b845ba2013-07-02 18:51:16 +0100249 .volatile_reg = s2mps11_volatile,
250 .cache_type = REGCACHE_FLAT,
Mark Brown25f311f2013-07-01 23:02:19 +0100251};
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800252
Krzysztof Kozlowskic2c9f1f2015-10-07 09:44:39 +0900253static void sec_pmic_dump_rev(struct sec_pmic_dev *sec_pmic)
254{
255 unsigned int val;
256
257 /* For each device type, the REG_ID is always the first register */
258 if (!regmap_read(sec_pmic->regmap_pmic, S2MPS11_REG_ID, &val))
259 dev_dbg(sec_pmic->dev, "Revision: 0x%x\n", val);
260}
261
Krzysztof Kozlowski8a97d422015-10-07 09:44:41 +0900262static void sec_pmic_configure(struct sec_pmic_dev *sec_pmic)
263{
264 int err;
265
266 if (sec_pmic->device_type != S2MPS13X)
267 return;
268
269 if (sec_pmic->pdata->disable_wrstbi) {
270 /*
271 * If WRSTBI pin is pulled down this feature must be disabled
272 * because each Suspend to RAM will trigger buck voltage reset
273 * to default values.
274 */
275 err = regmap_update_bits(sec_pmic->regmap_pmic,
276 S2MPS13_REG_WRSTBI,
277 S2MPS13_REG_WRSTBI_MASK, 0x0);
278 if (err)
279 dev_warn(sec_pmic->dev,
280 "Cannot initialize WRSTBI config: %d\n",
281 err);
282 }
283}
284
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800285#ifdef CONFIG_OF
286/*
287 * Only the common platform data elements for s5m8767 are parsed here from the
288 * device tree. Other sub-modules of s5m8767 such as pmic, rtc , charger and
289 * others have to parse their own platform data elements from device tree.
290 *
291 * The s5m8767 platform data structure is instantiated here and the drivers for
292 * the sub-modules need not instantiate another instance while parsing their
293 * platform data.
294 */
295static struct sec_platform_data *sec_pmic_i2c_parse_dt_pdata(
296 struct device *dev)
297{
298 struct sec_platform_data *pd;
299
300 pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
Krzysztof Kozlowski82a00c42015-02-26 10:13:51 +0100301 if (!pd)
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800302 return ERR_PTR(-ENOMEM);
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800303
304 /*
305 * ToDo: the 'wakeup' member in the platform data is more of a linux
306 * specfic information. Hence, there is no binding for that yet and
307 * not parsed here.
308 */
309
Krzysztof Kozlowskid7853342015-09-14 21:12:45 +0900310 pd->manual_poweroff = of_property_read_bool(dev->of_node,
311 "samsung,s2mps11-acokb-ground");
Krzysztof Kozlowski8a97d422015-10-07 09:44:41 +0900312 pd->disable_wrstbi = of_property_read_bool(dev->of_node,
313 "samsung,s2mps11-wrstbi-ground");
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800314 return pd;
315}
316#else
Mark Brown197bf852013-02-04 18:31:25 +0000317static struct sec_platform_data *sec_pmic_i2c_parse_dt_pdata(
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800318 struct device *dev)
319{
Wei Yongjun005718c2013-12-07 14:02:15 +0800320 return NULL;
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800321}
322#endif
323
Pankaj Dubey8f695de2014-03-13 11:14:07 +0900324static inline unsigned long sec_i2c_get_driver_data(struct i2c_client *i2c,
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800325 const struct i2c_device_id *id)
326{
327#ifdef CONFIG_OF
328 if (i2c->dev.of_node) {
329 const struct of_device_id *match;
Lee Jonesb6c1cb42014-07-21 13:09:25 +0100330
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800331 match = of_match_node(sec_dt_match, i2c->dev.of_node);
Pankaj Dubey8f695de2014-03-13 11:14:07 +0900332 return (unsigned long)match->data;
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800333 }
334#endif
Pankaj Dubey8f695de2014-03-13 11:14:07 +0900335 return id->driver_data;
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800336}
337
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900338static int sec_pmic_probe(struct i2c_client *i2c,
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900339 const struct i2c_device_id *id)
340{
Jingoo Han334a41c2013-07-30 17:10:05 +0900341 struct sec_platform_data *pdata = dev_get_platdata(&i2c->dev);
Krzysztof Kozlowskie349c912014-04-14 09:40:45 +0200342 const struct regmap_config *regmap;
Chanwoo Choi54e88272014-06-25 16:14:44 +0900343 const struct mfd_cell *sec_devs;
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900344 struct sec_pmic_dev *sec_pmic;
Krzysztof Kozlowski9549b5f2014-04-23 16:13:05 +0200345 unsigned long device_type;
Chanwoo Choi54e88272014-06-25 16:14:44 +0900346 int ret, num_sec_devs;
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900347
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900348 sec_pmic = devm_kzalloc(&i2c->dev, sizeof(struct sec_pmic_dev),
Sangbeom Kim621210e2012-01-20 16:09:11 +0900349 GFP_KERNEL);
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900350 if (sec_pmic == NULL)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900351 return -ENOMEM;
352
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900353 i2c_set_clientdata(i2c, sec_pmic);
354 sec_pmic->dev = &i2c->dev;
355 sec_pmic->i2c = i2c;
356 sec_pmic->irq = i2c->irq;
Krzysztof Kozlowski9549b5f2014-04-23 16:13:05 +0200357 device_type = sec_i2c_get_driver_data(i2c, id);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900358
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800359 if (sec_pmic->dev->of_node) {
360 pdata = sec_pmic_i2c_parse_dt_pdata(sec_pmic->dev);
361 if (IS_ERR(pdata)) {
362 ret = PTR_ERR(pdata);
363 return ret;
364 }
Krzysztof Kozlowski9549b5f2014-04-23 16:13:05 +0200365 pdata->device_type = device_type;
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800366 }
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900367 if (pdata) {
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900368 sec_pmic->device_type = pdata->device_type;
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900369 sec_pmic->irq_base = pdata->irq_base;
370 sec_pmic->wakeup = pdata->wakeup;
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800371 sec_pmic->pdata = pdata;
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900372 }
373
Mark Brown25f311f2013-07-01 23:02:19 +0100374 switch (sec_pmic->device_type) {
Sachin Kamatce249912014-03-14 17:21:57 +0530375 case S2MPA01:
376 regmap = &s2mpa01_regmap_config;
377 break;
Mark Brown25f311f2013-07-01 23:02:19 +0100378 case S2MPS11X:
379 regmap = &s2mps11_regmap_config;
Krzysztof Kozlowskidc691962014-02-28 11:41:44 +0100380 break;
Chanwoo Choi76b98402014-11-18 17:59:40 +0900381 case S2MPS13X:
382 regmap = &s2mps13_regmap_config;
383 break;
Krzysztof Kozlowskidc691962014-02-28 11:41:44 +0100384 case S2MPS14X:
385 regmap = &s2mps14_regmap_config;
Mark Brown25f311f2013-07-01 23:02:19 +0100386 break;
387 case S5M8763X:
388 regmap = &s5m8763_regmap_config;
389 break;
390 case S5M8767X:
391 regmap = &s5m8767_regmap_config;
392 break;
Chanwoo Choi00e25732014-06-25 16:14:45 +0900393 case S2MPU02:
394 regmap = &s2mpu02_regmap_config;
395 break;
Mark Brown25f311f2013-07-01 23:02:19 +0100396 default:
397 regmap = &sec_regmap_config;
398 break;
399 }
400
Krzysztof Kozlowski3e1e4a52013-12-12 17:12:31 -0800401 sec_pmic->regmap_pmic = devm_regmap_init_i2c(i2c, regmap);
402 if (IS_ERR(sec_pmic->regmap_pmic)) {
403 ret = PTR_ERR(sec_pmic->regmap_pmic);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900404 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
Axel Lin61a2af32012-02-28 14:23:55 +0800405 ret);
Axel Lin1092e1c2012-04-25 10:03:44 +0800406 return ret;
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900407 }
408
Jonghwan Choic3ebb302012-01-16 09:08:42 +0900409 if (pdata && pdata->cfg_pmic_irq)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900410 pdata->cfg_pmic_irq();
411
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900412 sec_irq_init(sec_pmic);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900413
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900414 pm_runtime_set_active(sec_pmic->dev);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900415
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900416 switch (sec_pmic->device_type) {
Sangbeom Kim88a1cfd2012-01-20 16:09:12 +0900417 case S5M8751X:
Chanwoo Choi54e88272014-06-25 16:14:44 +0900418 sec_devs = s5m8751_devs;
419 num_sec_devs = ARRAY_SIZE(s5m8751_devs);
Sangbeom Kim88a1cfd2012-01-20 16:09:12 +0900420 break;
421 case S5M8763X:
Chanwoo Choi54e88272014-06-25 16:14:44 +0900422 sec_devs = s5m8763_devs;
423 num_sec_devs = ARRAY_SIZE(s5m8763_devs);
Sangbeom Kim88a1cfd2012-01-20 16:09:12 +0900424 break;
425 case S5M8767X:
Chanwoo Choi54e88272014-06-25 16:14:44 +0900426 sec_devs = s5m8767_devs;
427 num_sec_devs = ARRAY_SIZE(s5m8767_devs);
Sangbeom Kim88a1cfd2012-01-20 16:09:12 +0900428 break;
Sachin Kamatce249912014-03-14 17:21:57 +0530429 case S2MPA01:
Chanwoo Choi54e88272014-06-25 16:14:44 +0900430 sec_devs = s2mpa01_devs;
431 num_sec_devs = ARRAY_SIZE(s2mpa01_devs);
Sachin Kamatce249912014-03-14 17:21:57 +0530432 break;
Sangbeom Kim9b6d1342012-07-11 21:07:55 +0900433 case S2MPS11X:
Chanwoo Choi54e88272014-06-25 16:14:44 +0900434 sec_devs = s2mps11_devs;
435 num_sec_devs = ARRAY_SIZE(s2mps11_devs);
Sangbeom Kim9b6d1342012-07-11 21:07:55 +0900436 break;
Chanwoo Choi3bc2ee92014-11-18 17:59:39 +0900437 case S2MPS13X:
438 sec_devs = s2mps13_devs;
439 num_sec_devs = ARRAY_SIZE(s2mps13_devs);
440 break;
Krzysztof Kozlowskidc691962014-02-28 11:41:44 +0100441 case S2MPS14X:
Chanwoo Choi54e88272014-06-25 16:14:44 +0900442 sec_devs = s2mps14_devs;
443 num_sec_devs = ARRAY_SIZE(s2mps14_devs);
444 break;
445 case S2MPU02:
446 sec_devs = s2mpu02_devs;
447 num_sec_devs = ARRAY_SIZE(s2mpu02_devs);
Krzysztof Kozlowskidc691962014-02-28 11:41:44 +0100448 break;
Sangbeom Kim88a1cfd2012-01-20 16:09:12 +0900449 default:
450 /* If this happens the probe function is problem */
451 BUG();
452 }
Chanwoo Choi54e88272014-06-25 16:14:44 +0900453 ret = mfd_add_devices(sec_pmic->dev, -1, sec_devs, num_sec_devs, NULL,
454 0, NULL);
Leon Romanovsky15447a462013-05-15 18:00:48 +0300455 if (ret)
Krzysztof Kozlowski8c66eec2014-03-18 13:57:50 +0100456 goto err_mfd;
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900457
Krzysztof Kozlowskif6d6daa2013-11-26 14:50:27 +0100458 device_init_wakeup(sec_pmic->dev, sec_pmic->wakeup);
Krzysztof Kozlowski8a97d422015-10-07 09:44:41 +0900459 sec_pmic_configure(sec_pmic);
Krzysztof Kozlowskic2c9f1f2015-10-07 09:44:39 +0900460 sec_pmic_dump_rev(sec_pmic);
Krzysztof Kozlowskif6d6daa2013-11-26 14:50:27 +0100461
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900462 return ret;
463
Krzysztof Kozlowski8c66eec2014-03-18 13:57:50 +0100464err_mfd:
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900465 sec_irq_exit(sec_pmic);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900466 return ret;
467}
468
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900469static int sec_pmic_remove(struct i2c_client *i2c)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900470{
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900471 struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900472
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900473 mfd_remove_devices(sec_pmic->dev);
474 sec_irq_exit(sec_pmic);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900475 return 0;
476}
477
Krzysztof Kozlowskid7853342015-09-14 21:12:45 +0900478static void sec_pmic_shutdown(struct i2c_client *i2c)
479{
480 struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
481 unsigned int reg, mask;
482
483 if (!sec_pmic->pdata->manual_poweroff)
484 return;
485
486 switch (sec_pmic->device_type) {
487 case S2MPS11X:
488 reg = S2MPS11_REG_CTRL1;
489 mask = S2MPS11_CTRL1_PWRHOLD_MASK;
490 break;
491 default:
492 /*
493 * Currently only one board with S2MPS11 needs this, so just
494 * ignore the rest.
495 */
496 dev_warn(sec_pmic->dev,
497 "Unsupported device %lu for manual power off\n",
498 sec_pmic->device_type);
499 return;
500 }
501
502 regmap_update_bits(sec_pmic->regmap_pmic, reg, mask, 0);
503}
504
Geert Uytterhoeven8321bbf2014-01-26 11:38:43 +0100505#ifdef CONFIG_PM_SLEEP
Krzysztof Kozlowskif6d6daa2013-11-26 14:50:27 +0100506static int sec_pmic_suspend(struct device *dev)
507{
508 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
509 struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
510
Krzysztof Kozlowski360d15d2014-04-22 16:00:25 +0200511 if (device_may_wakeup(dev))
Krzysztof Kozlowskif6d6daa2013-11-26 14:50:27 +0100512 enable_irq_wake(sec_pmic->irq);
Krzysztof Kozlowski360d15d2014-04-22 16:00:25 +0200513 /*
514 * PMIC IRQ must be disabled during suspend for RTC alarm
515 * to work properly.
516 * When device is woken up from suspend, an
517 * interrupt occurs before resuming I2C bus controller.
518 * The interrupt is handled by regmap_irq_thread which tries
519 * to read RTC registers. This read fails (I2C is still
520 * suspended) and RTC Alarm interrupt is disabled.
521 */
522 disable_irq(sec_pmic->irq);
Krzysztof Kozlowskif6d6daa2013-11-26 14:50:27 +0100523
524 return 0;
525}
526
527static int sec_pmic_resume(struct device *dev)
528{
529 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
530 struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
531
Krzysztof Kozlowski360d15d2014-04-22 16:00:25 +0200532 if (device_may_wakeup(dev))
Krzysztof Kozlowskif6d6daa2013-11-26 14:50:27 +0100533 disable_irq_wake(sec_pmic->irq);
Krzysztof Kozlowski360d15d2014-04-22 16:00:25 +0200534 enable_irq(sec_pmic->irq);
Krzysztof Kozlowskif6d6daa2013-11-26 14:50:27 +0100535
536 return 0;
537}
Geert Uytterhoeven8321bbf2014-01-26 11:38:43 +0100538#endif /* CONFIG_PM_SLEEP */
Krzysztof Kozlowskif6d6daa2013-11-26 14:50:27 +0100539
540static SIMPLE_DEV_PM_OPS(sec_pmic_pm_ops, sec_pmic_suspend, sec_pmic_resume);
541
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900542static const struct i2c_device_id sec_pmic_id[] = {
543 { "sec_pmic", 0 },
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900544 { }
545};
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900546MODULE_DEVICE_TABLE(i2c, sec_pmic_id);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900547
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900548static struct i2c_driver sec_pmic_driver = {
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900549 .driver = {
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900550 .name = "sec_pmic",
Krzysztof Kozlowskif6d6daa2013-11-26 14:50:27 +0100551 .pm = &sec_pmic_pm_ops,
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800552 .of_match_table = of_match_ptr(sec_dt_match),
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900553 },
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900554 .probe = sec_pmic_probe,
555 .remove = sec_pmic_remove,
Krzysztof Kozlowskid7853342015-09-14 21:12:45 +0900556 .shutdown = sec_pmic_shutdown,
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900557 .id_table = sec_pmic_id,
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900558};
559
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900560static int __init sec_pmic_init(void)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900561{
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900562 return i2c_add_driver(&sec_pmic_driver);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900563}
564
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900565subsys_initcall(sec_pmic_init);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900566
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900567static void __exit sec_pmic_exit(void)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900568{
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900569 i2c_del_driver(&sec_pmic_driver);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900570}
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900571module_exit(sec_pmic_exit);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900572
573MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
574MODULE_DESCRIPTION("Core support for the S5M MFD");
575MODULE_LICENSE("GPL");