blob: 34c18fb8c0896b46f49de48b4d5631a5d780a811 [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>
28#include <linux/mfd/samsung/rtc.h>
Mark Brown25f311f2013-07-01 23:02:19 +010029#include <linux/mfd/samsung/s2mps11.h>
30#include <linux/mfd/samsung/s5m8763.h>
31#include <linux/mfd/samsung/s5m8767.h>
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090032#include <linux/regmap.h>
33
Sangbeom Kim88a1cfd2012-01-20 16:09:12 +090034static struct mfd_cell s5m8751_devs[] = {
35 {
36 .name = "s5m8751-pmic",
37 }, {
38 .name = "s5m-charger",
39 }, {
40 .name = "s5m8751-codec",
41 },
42};
43
44static struct mfd_cell s5m8763_devs[] = {
45 {
46 .name = "s5m8763-pmic",
47 }, {
48 .name = "s5m-rtc",
49 }, {
50 .name = "s5m-charger",
51 },
52};
53
54static struct mfd_cell s5m8767_devs[] = {
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090055 {
56 .name = "s5m8767-pmic",
57 }, {
58 .name = "s5m-rtc",
59 },
60};
61
Sangbeom Kim9b6d1342012-07-11 21:07:55 +090062static struct mfd_cell s2mps11_devs[] = {
63 {
64 .name = "s2mps11-pmic",
Yadwinder Singh Brar3134bca2013-07-07 17:14:21 +053065 }, {
66 .name = "s2mps11-clk",
67 }
Sangbeom Kim9b6d1342012-07-11 21:07:55 +090068};
69
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -080070#ifdef CONFIG_OF
71static struct of_device_id sec_dt_match[] = {
72 { .compatible = "samsung,s5m8767-pmic",
73 .data = (void *)S5M8767X,
74 },
Yadwinder Singh Braraa32aca2013-06-29 18:21:20 +053075 { .compatible = "samsung,s2mps11-pmic",
76 .data = (void *)S2MPS11X,
77 },
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -080078 {},
79};
80#endif
81
Sangbeom Kim63063bf2012-07-11 21:06:55 +090082int sec_reg_read(struct sec_pmic_dev *sec_pmic, u8 reg, void *dest)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090083{
Sangbeom Kim63063bf2012-07-11 21:06:55 +090084 return regmap_read(sec_pmic->regmap, reg, dest);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090085}
Sangbeom Kim63063bf2012-07-11 21:06:55 +090086EXPORT_SYMBOL_GPL(sec_reg_read);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090087
Sangbeom Kim63063bf2012-07-11 21:06:55 +090088int sec_bulk_read(struct sec_pmic_dev *sec_pmic, u8 reg, int count, u8 *buf)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090089{
Sangbeom Kim63063bf2012-07-11 21:06:55 +090090 return regmap_bulk_read(sec_pmic->regmap, reg, buf, count);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090091}
Sangbeom Kim63063bf2012-07-11 21:06:55 +090092EXPORT_SYMBOL_GPL(sec_bulk_read);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090093
Sangbeom Kim63063bf2012-07-11 21:06:55 +090094int sec_reg_write(struct sec_pmic_dev *sec_pmic, u8 reg, u8 value)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090095{
Sangbeom Kim63063bf2012-07-11 21:06:55 +090096 return regmap_write(sec_pmic->regmap, reg, value);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090097}
Sangbeom Kim63063bf2012-07-11 21:06:55 +090098EXPORT_SYMBOL_GPL(sec_reg_write);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090099
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900100int sec_bulk_write(struct sec_pmic_dev *sec_pmic, u8 reg, int count, u8 *buf)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900101{
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900102 return regmap_raw_write(sec_pmic->regmap, reg, buf, count);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900103}
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900104EXPORT_SYMBOL_GPL(sec_bulk_write);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900105
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900106int sec_reg_update(struct sec_pmic_dev *sec_pmic, u8 reg, u8 val, u8 mask)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900107{
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900108 return regmap_update_bits(sec_pmic->regmap, reg, mask, val);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900109}
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900110EXPORT_SYMBOL_GPL(sec_reg_update);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900111
Mark Brown6b845ba2013-07-02 18:51:16 +0100112static bool s2mps11_volatile(struct device *dev, unsigned int reg)
113{
114 switch (reg) {
115 case S2MPS11_REG_INT1M:
116 case S2MPS11_REG_INT2M:
117 case S2MPS11_REG_INT3M:
118 return false;
119 default:
120 return true;
121 }
122}
123
124static bool s5m8763_volatile(struct device *dev, unsigned int reg)
125{
126 switch (reg) {
127 case S5M8763_REG_IRQM1:
128 case S5M8763_REG_IRQM2:
129 case S5M8763_REG_IRQM3:
130 case S5M8763_REG_IRQM4:
131 return false;
132 default:
133 return true;
134 }
135}
136
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900137static struct regmap_config sec_regmap_config = {
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900138 .reg_bits = 8,
139 .val_bits = 8,
140};
141
Mark Brown25f311f2013-07-01 23:02:19 +0100142static struct regmap_config s2mps11_regmap_config = {
143 .reg_bits = 8,
144 .val_bits = 8,
145
146 .max_register = S2MPS11_REG_L38CTRL,
Mark Brown6b845ba2013-07-02 18:51:16 +0100147 .volatile_reg = s2mps11_volatile,
148 .cache_type = REGCACHE_FLAT,
Mark Brown25f311f2013-07-01 23:02:19 +0100149};
150
151static struct regmap_config s5m8763_regmap_config = {
152 .reg_bits = 8,
153 .val_bits = 8,
154
155 .max_register = S5M8763_REG_LBCNFG2,
Mark Brown6b845ba2013-07-02 18:51:16 +0100156 .volatile_reg = s5m8763_volatile,
157 .cache_type = REGCACHE_FLAT,
Mark Brown25f311f2013-07-01 23:02:19 +0100158};
159
160static struct regmap_config s5m8767_regmap_config = {
161 .reg_bits = 8,
162 .val_bits = 8,
163
164 .max_register = S5M8767_REG_LDO28CTRL,
Mark Brown6b845ba2013-07-02 18:51:16 +0100165 .volatile_reg = s2mps11_volatile,
166 .cache_type = REGCACHE_FLAT,
Mark Brown25f311f2013-07-01 23:02:19 +0100167};
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800168
169#ifdef CONFIG_OF
170/*
171 * Only the common platform data elements for s5m8767 are parsed here from the
172 * device tree. Other sub-modules of s5m8767 such as pmic, rtc , charger and
173 * others have to parse their own platform data elements from device tree.
174 *
175 * The s5m8767 platform data structure is instantiated here and the drivers for
176 * the sub-modules need not instantiate another instance while parsing their
177 * platform data.
178 */
179static struct sec_platform_data *sec_pmic_i2c_parse_dt_pdata(
180 struct device *dev)
181{
182 struct sec_platform_data *pd;
183
184 pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
185 if (!pd) {
186 dev_err(dev, "could not allocate memory for pdata\n");
187 return ERR_PTR(-ENOMEM);
188 }
189
190 /*
191 * ToDo: the 'wakeup' member in the platform data is more of a linux
192 * specfic information. Hence, there is no binding for that yet and
193 * not parsed here.
194 */
195
196 return pd;
197}
198#else
Mark Brown197bf852013-02-04 18:31:25 +0000199static struct sec_platform_data *sec_pmic_i2c_parse_dt_pdata(
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800200 struct device *dev)
201{
202 return 0;
203}
204#endif
205
206static inline int sec_i2c_get_driver_data(struct i2c_client *i2c,
207 const struct i2c_device_id *id)
208{
209#ifdef CONFIG_OF
210 if (i2c->dev.of_node) {
211 const struct of_device_id *match;
212 match = of_match_node(sec_dt_match, i2c->dev.of_node);
213 return (int)match->data;
214 }
215#endif
216 return (int)id->driver_data;
217}
218
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900219static int sec_pmic_probe(struct i2c_client *i2c,
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900220 const struct i2c_device_id *id)
221{
Jingoo Han334a41c2013-07-30 17:10:05 +0900222 struct sec_platform_data *pdata = dev_get_platdata(&i2c->dev);
Mark Brown25f311f2013-07-01 23:02:19 +0100223 const struct regmap_config *regmap;
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900224 struct sec_pmic_dev *sec_pmic;
Axel Lin61a2af32012-02-28 14:23:55 +0800225 int ret;
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900226
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900227 sec_pmic = devm_kzalloc(&i2c->dev, sizeof(struct sec_pmic_dev),
Sangbeom Kim621210e2012-01-20 16:09:11 +0900228 GFP_KERNEL);
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900229 if (sec_pmic == NULL)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900230 return -ENOMEM;
231
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900232 i2c_set_clientdata(i2c, sec_pmic);
233 sec_pmic->dev = &i2c->dev;
234 sec_pmic->i2c = i2c;
235 sec_pmic->irq = i2c->irq;
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800236 sec_pmic->type = sec_i2c_get_driver_data(i2c, id);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900237
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800238 if (sec_pmic->dev->of_node) {
239 pdata = sec_pmic_i2c_parse_dt_pdata(sec_pmic->dev);
240 if (IS_ERR(pdata)) {
241 ret = PTR_ERR(pdata);
242 return ret;
243 }
244 pdata->device_type = sec_pmic->type;
245 }
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900246 if (pdata) {
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900247 sec_pmic->device_type = pdata->device_type;
248 sec_pmic->ono = pdata->ono;
249 sec_pmic->irq_base = pdata->irq_base;
250 sec_pmic->wakeup = pdata->wakeup;
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800251 sec_pmic->pdata = pdata;
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900252 }
253
Mark Brown25f311f2013-07-01 23:02:19 +0100254 switch (sec_pmic->device_type) {
255 case S2MPS11X:
256 regmap = &s2mps11_regmap_config;
257 break;
258 case S5M8763X:
259 regmap = &s5m8763_regmap_config;
260 break;
261 case S5M8767X:
262 regmap = &s5m8767_regmap_config;
263 break;
264 default:
265 regmap = &sec_regmap_config;
266 break;
267 }
268
269 sec_pmic->regmap = devm_regmap_init_i2c(i2c, regmap);
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900270 if (IS_ERR(sec_pmic->regmap)) {
271 ret = PTR_ERR(sec_pmic->regmap);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900272 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
Axel Lin61a2af32012-02-28 14:23:55 +0800273 ret);
Axel Lin1092e1c2012-04-25 10:03:44 +0800274 return ret;
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900275 }
276
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900277 sec_pmic->rtc = i2c_new_dummy(i2c->adapter, RTC_I2C_ADDR);
278 i2c_set_clientdata(sec_pmic->rtc, sec_pmic);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900279
Jonghwan Choic3ebb302012-01-16 09:08:42 +0900280 if (pdata && pdata->cfg_pmic_irq)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900281 pdata->cfg_pmic_irq();
282
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900283 sec_irq_init(sec_pmic);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900284
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900285 pm_runtime_set_active(sec_pmic->dev);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900286
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900287 switch (sec_pmic->device_type) {
Sangbeom Kim88a1cfd2012-01-20 16:09:12 +0900288 case S5M8751X:
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900289 ret = mfd_add_devices(sec_pmic->dev, -1, s5m8751_devs,
Mark Brown0848c942012-09-11 15:16:36 +0800290 ARRAY_SIZE(s5m8751_devs), NULL, 0, NULL);
Sangbeom Kim88a1cfd2012-01-20 16:09:12 +0900291 break;
292 case S5M8763X:
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900293 ret = mfd_add_devices(sec_pmic->dev, -1, s5m8763_devs,
Mark Brown0848c942012-09-11 15:16:36 +0800294 ARRAY_SIZE(s5m8763_devs), NULL, 0, NULL);
Sangbeom Kim88a1cfd2012-01-20 16:09:12 +0900295 break;
296 case S5M8767X:
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900297 ret = mfd_add_devices(sec_pmic->dev, -1, s5m8767_devs,
Mark Brown0848c942012-09-11 15:16:36 +0800298 ARRAY_SIZE(s5m8767_devs), NULL, 0, NULL);
Sangbeom Kim88a1cfd2012-01-20 16:09:12 +0900299 break;
Sangbeom Kim9b6d1342012-07-11 21:07:55 +0900300 case S2MPS11X:
301 ret = mfd_add_devices(sec_pmic->dev, -1, s2mps11_devs,
Mark Brown0848c942012-09-11 15:16:36 +0800302 ARRAY_SIZE(s2mps11_devs), NULL, 0, NULL);
Sangbeom Kim9b6d1342012-07-11 21:07:55 +0900303 break;
Sangbeom Kim88a1cfd2012-01-20 16:09:12 +0900304 default:
305 /* If this happens the probe function is problem */
306 BUG();
307 }
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900308
Leon Romanovsky15447a462013-05-15 18:00:48 +0300309 if (ret)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900310 goto err;
311
312 return ret;
313
314err:
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900315 sec_irq_exit(sec_pmic);
316 i2c_unregister_device(sec_pmic->rtc);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900317 return ret;
318}
319
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900320static int sec_pmic_remove(struct i2c_client *i2c)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900321{
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900322 struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900323
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900324 mfd_remove_devices(sec_pmic->dev);
325 sec_irq_exit(sec_pmic);
326 i2c_unregister_device(sec_pmic->rtc);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900327 return 0;
328}
329
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900330static const struct i2c_device_id sec_pmic_id[] = {
331 { "sec_pmic", 0 },
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900332 { }
333};
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900334MODULE_DEVICE_TABLE(i2c, sec_pmic_id);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900335
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900336static struct i2c_driver sec_pmic_driver = {
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900337 .driver = {
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900338 .name = "sec_pmic",
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900339 .owner = THIS_MODULE,
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800340 .of_match_table = of_match_ptr(sec_dt_match),
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900341 },
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900342 .probe = sec_pmic_probe,
343 .remove = sec_pmic_remove,
344 .id_table = sec_pmic_id,
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900345};
346
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900347static int __init sec_pmic_init(void)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900348{
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900349 return i2c_add_driver(&sec_pmic_driver);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900350}
351
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900352subsys_initcall(sec_pmic_init);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900353
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900354static void __exit sec_pmic_exit(void)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900355{
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900356 i2c_del_driver(&sec_pmic_driver);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900357}
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900358module_exit(sec_pmic_exit);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900359
360MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
361MODULE_DESCRIPTION("Core support for the S5M MFD");
362MODULE_LICENSE("GPL");