blob: 79c07502db5250fc2b045abeea2ad99890b2c23a [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>
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -080020#include <linux/of_irq.h>
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090021#include <linux/interrupt.h>
22#include <linux/pm_runtime.h>
23#include <linux/mutex.h>
24#include <linux/mfd/core.h>
Sangbeom Kim54227bc2012-07-11 21:07:16 +090025#include <linux/mfd/samsung/core.h>
26#include <linux/mfd/samsung/irq.h>
27#include <linux/mfd/samsung/rtc.h>
Mark Brown25f311f2013-07-01 23:02:19 +010028#include <linux/mfd/samsung/s2mps11.h>
29#include <linux/mfd/samsung/s5m8763.h>
30#include <linux/mfd/samsung/s5m8767.h>
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090031#include <linux/regmap.h>
32
Sangbeom Kim88a1cfd2012-01-20 16:09:12 +090033static struct mfd_cell s5m8751_devs[] = {
34 {
35 .name = "s5m8751-pmic",
36 }, {
37 .name = "s5m-charger",
38 }, {
39 .name = "s5m8751-codec",
40 },
41};
42
43static struct mfd_cell s5m8763_devs[] = {
44 {
45 .name = "s5m8763-pmic",
46 }, {
47 .name = "s5m-rtc",
48 }, {
49 .name = "s5m-charger",
50 },
51};
52
53static struct mfd_cell s5m8767_devs[] = {
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090054 {
55 .name = "s5m8767-pmic",
56 }, {
57 .name = "s5m-rtc",
58 },
59};
60
Sangbeom Kim9b6d1342012-07-11 21:07:55 +090061static struct mfd_cell s2mps11_devs[] = {
62 {
63 .name = "s2mps11-pmic",
64 },
65};
66
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -080067#ifdef CONFIG_OF
68static struct of_device_id sec_dt_match[] = {
69 { .compatible = "samsung,s5m8767-pmic",
70 .data = (void *)S5M8767X,
71 },
Yadwinder Singh Braraa32aca2013-06-29 18:21:20 +053072 { .compatible = "samsung,s2mps11-pmic",
73 .data = (void *)S2MPS11X,
74 },
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -080075 {},
76};
77#endif
78
Sangbeom Kim63063bf2012-07-11 21:06:55 +090079int sec_reg_read(struct sec_pmic_dev *sec_pmic, u8 reg, void *dest)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090080{
Sangbeom Kim63063bf2012-07-11 21:06:55 +090081 return regmap_read(sec_pmic->regmap, reg, dest);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090082}
Sangbeom Kim63063bf2012-07-11 21:06:55 +090083EXPORT_SYMBOL_GPL(sec_reg_read);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090084
Sangbeom Kim63063bf2012-07-11 21:06:55 +090085int sec_bulk_read(struct sec_pmic_dev *sec_pmic, u8 reg, int count, u8 *buf)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090086{
Sangbeom Kim63063bf2012-07-11 21:06:55 +090087 return regmap_bulk_read(sec_pmic->regmap, reg, buf, count);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090088}
Sangbeom Kim63063bf2012-07-11 21:06:55 +090089EXPORT_SYMBOL_GPL(sec_bulk_read);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090090
Sangbeom Kim63063bf2012-07-11 21:06:55 +090091int sec_reg_write(struct sec_pmic_dev *sec_pmic, u8 reg, u8 value)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090092{
Sangbeom Kim63063bf2012-07-11 21:06:55 +090093 return regmap_write(sec_pmic->regmap, reg, value);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090094}
Sangbeom Kim63063bf2012-07-11 21:06:55 +090095EXPORT_SYMBOL_GPL(sec_reg_write);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090096
Sangbeom Kim63063bf2012-07-11 21:06:55 +090097int sec_bulk_write(struct sec_pmic_dev *sec_pmic, u8 reg, int count, u8 *buf)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090098{
Sangbeom Kim63063bf2012-07-11 21:06:55 +090099 return regmap_raw_write(sec_pmic->regmap, reg, buf, count);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900100}
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900101EXPORT_SYMBOL_GPL(sec_bulk_write);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900102
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900103int sec_reg_update(struct sec_pmic_dev *sec_pmic, u8 reg, u8 val, u8 mask)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900104{
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900105 return regmap_update_bits(sec_pmic->regmap, reg, mask, val);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900106}
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900107EXPORT_SYMBOL_GPL(sec_reg_update);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900108
Mark Brown6b845ba2013-07-02 18:51:16 +0100109static bool s2mps11_volatile(struct device *dev, unsigned int reg)
110{
111 switch (reg) {
112 case S2MPS11_REG_INT1M:
113 case S2MPS11_REG_INT2M:
114 case S2MPS11_REG_INT3M:
115 return false;
116 default:
117 return true;
118 }
119}
120
121static bool s5m8763_volatile(struct device *dev, unsigned int reg)
122{
123 switch (reg) {
124 case S5M8763_REG_IRQM1:
125 case S5M8763_REG_IRQM2:
126 case S5M8763_REG_IRQM3:
127 case S5M8763_REG_IRQM4:
128 return false;
129 default:
130 return true;
131 }
132}
133
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900134static struct regmap_config sec_regmap_config = {
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900135 .reg_bits = 8,
136 .val_bits = 8,
137};
138
Mark Brown25f311f2013-07-01 23:02:19 +0100139static struct regmap_config s2mps11_regmap_config = {
140 .reg_bits = 8,
141 .val_bits = 8,
142
143 .max_register = S2MPS11_REG_L38CTRL,
Mark Brown6b845ba2013-07-02 18:51:16 +0100144 .volatile_reg = s2mps11_volatile,
145 .cache_type = REGCACHE_FLAT,
Mark Brown25f311f2013-07-01 23:02:19 +0100146};
147
148static struct regmap_config s5m8763_regmap_config = {
149 .reg_bits = 8,
150 .val_bits = 8,
151
152 .max_register = S5M8763_REG_LBCNFG2,
Mark Brown6b845ba2013-07-02 18:51:16 +0100153 .volatile_reg = s5m8763_volatile,
154 .cache_type = REGCACHE_FLAT,
Mark Brown25f311f2013-07-01 23:02:19 +0100155};
156
157static struct regmap_config s5m8767_regmap_config = {
158 .reg_bits = 8,
159 .val_bits = 8,
160
161 .max_register = S5M8767_REG_LDO28CTRL,
Mark Brown6b845ba2013-07-02 18:51:16 +0100162 .volatile_reg = s2mps11_volatile,
163 .cache_type = REGCACHE_FLAT,
Mark Brown25f311f2013-07-01 23:02:19 +0100164};
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800165
166#ifdef CONFIG_OF
167/*
168 * Only the common platform data elements for s5m8767 are parsed here from the
169 * device tree. Other sub-modules of s5m8767 such as pmic, rtc , charger and
170 * others have to parse their own platform data elements from device tree.
171 *
172 * The s5m8767 platform data structure is instantiated here and the drivers for
173 * the sub-modules need not instantiate another instance while parsing their
174 * platform data.
175 */
176static struct sec_platform_data *sec_pmic_i2c_parse_dt_pdata(
177 struct device *dev)
178{
179 struct sec_platform_data *pd;
180
181 pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
182 if (!pd) {
183 dev_err(dev, "could not allocate memory for pdata\n");
184 return ERR_PTR(-ENOMEM);
185 }
186
187 /*
188 * ToDo: the 'wakeup' member in the platform data is more of a linux
189 * specfic information. Hence, there is no binding for that yet and
190 * not parsed here.
191 */
192
193 return pd;
194}
195#else
Mark Brown197bf852013-02-04 18:31:25 +0000196static struct sec_platform_data *sec_pmic_i2c_parse_dt_pdata(
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800197 struct device *dev)
198{
199 return 0;
200}
201#endif
202
203static inline int sec_i2c_get_driver_data(struct i2c_client *i2c,
204 const struct i2c_device_id *id)
205{
206#ifdef CONFIG_OF
207 if (i2c->dev.of_node) {
208 const struct of_device_id *match;
209 match = of_match_node(sec_dt_match, i2c->dev.of_node);
210 return (int)match->data;
211 }
212#endif
213 return (int)id->driver_data;
214}
215
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900216static int sec_pmic_probe(struct i2c_client *i2c,
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900217 const struct i2c_device_id *id)
218{
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900219 struct sec_platform_data *pdata = i2c->dev.platform_data;
Mark Brown25f311f2013-07-01 23:02:19 +0100220 const struct regmap_config *regmap;
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900221 struct sec_pmic_dev *sec_pmic;
Axel Lin61a2af32012-02-28 14:23:55 +0800222 int ret;
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900223
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900224 sec_pmic = devm_kzalloc(&i2c->dev, sizeof(struct sec_pmic_dev),
Sangbeom Kim621210e2012-01-20 16:09:11 +0900225 GFP_KERNEL);
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900226 if (sec_pmic == NULL)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900227 return -ENOMEM;
228
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900229 i2c_set_clientdata(i2c, sec_pmic);
230 sec_pmic->dev = &i2c->dev;
231 sec_pmic->i2c = i2c;
232 sec_pmic->irq = i2c->irq;
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800233 sec_pmic->type = sec_i2c_get_driver_data(i2c, id);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900234
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800235 if (sec_pmic->dev->of_node) {
236 pdata = sec_pmic_i2c_parse_dt_pdata(sec_pmic->dev);
237 if (IS_ERR(pdata)) {
238 ret = PTR_ERR(pdata);
239 return ret;
240 }
241 pdata->device_type = sec_pmic->type;
242 }
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900243 if (pdata) {
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900244 sec_pmic->device_type = pdata->device_type;
245 sec_pmic->ono = pdata->ono;
246 sec_pmic->irq_base = pdata->irq_base;
247 sec_pmic->wakeup = pdata->wakeup;
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800248 sec_pmic->pdata = pdata;
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900249 }
250
Mark Brown25f311f2013-07-01 23:02:19 +0100251 switch (sec_pmic->device_type) {
252 case S2MPS11X:
253 regmap = &s2mps11_regmap_config;
254 break;
255 case S5M8763X:
256 regmap = &s5m8763_regmap_config;
257 break;
258 case S5M8767X:
259 regmap = &s5m8767_regmap_config;
260 break;
261 default:
262 regmap = &sec_regmap_config;
263 break;
264 }
265
266 sec_pmic->regmap = devm_regmap_init_i2c(i2c, regmap);
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900267 if (IS_ERR(sec_pmic->regmap)) {
268 ret = PTR_ERR(sec_pmic->regmap);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900269 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
Axel Lin61a2af32012-02-28 14:23:55 +0800270 ret);
Axel Lin1092e1c2012-04-25 10:03:44 +0800271 return ret;
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900272 }
273
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900274 sec_pmic->rtc = i2c_new_dummy(i2c->adapter, RTC_I2C_ADDR);
275 i2c_set_clientdata(sec_pmic->rtc, sec_pmic);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900276
Jonghwan Choic3ebb302012-01-16 09:08:42 +0900277 if (pdata && pdata->cfg_pmic_irq)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900278 pdata->cfg_pmic_irq();
279
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900280 sec_irq_init(sec_pmic);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900281
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900282 pm_runtime_set_active(sec_pmic->dev);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900283
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900284 switch (sec_pmic->device_type) {
Sangbeom Kim88a1cfd2012-01-20 16:09:12 +0900285 case S5M8751X:
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900286 ret = mfd_add_devices(sec_pmic->dev, -1, s5m8751_devs,
Mark Brown0848c942012-09-11 15:16:36 +0800287 ARRAY_SIZE(s5m8751_devs), NULL, 0, NULL);
Sangbeom Kim88a1cfd2012-01-20 16:09:12 +0900288 break;
289 case S5M8763X:
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900290 ret = mfd_add_devices(sec_pmic->dev, -1, s5m8763_devs,
Mark Brown0848c942012-09-11 15:16:36 +0800291 ARRAY_SIZE(s5m8763_devs), NULL, 0, NULL);
Sangbeom Kim88a1cfd2012-01-20 16:09:12 +0900292 break;
293 case S5M8767X:
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900294 ret = mfd_add_devices(sec_pmic->dev, -1, s5m8767_devs,
Mark Brown0848c942012-09-11 15:16:36 +0800295 ARRAY_SIZE(s5m8767_devs), NULL, 0, NULL);
Sangbeom Kim88a1cfd2012-01-20 16:09:12 +0900296 break;
Sangbeom Kim9b6d1342012-07-11 21:07:55 +0900297 case S2MPS11X:
298 ret = mfd_add_devices(sec_pmic->dev, -1, s2mps11_devs,
Mark Brown0848c942012-09-11 15:16:36 +0800299 ARRAY_SIZE(s2mps11_devs), NULL, 0, NULL);
Sangbeom Kim9b6d1342012-07-11 21:07:55 +0900300 break;
Sangbeom Kim88a1cfd2012-01-20 16:09:12 +0900301 default:
302 /* If this happens the probe function is problem */
303 BUG();
304 }
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900305
Leon Romanovsky15447a462013-05-15 18:00:48 +0300306 if (ret)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900307 goto err;
308
309 return ret;
310
311err:
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900312 sec_irq_exit(sec_pmic);
313 i2c_unregister_device(sec_pmic->rtc);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900314 return ret;
315}
316
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900317static int sec_pmic_remove(struct i2c_client *i2c)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900318{
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900319 struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900320
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900321 mfd_remove_devices(sec_pmic->dev);
322 sec_irq_exit(sec_pmic);
323 i2c_unregister_device(sec_pmic->rtc);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900324 return 0;
325}
326
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900327static const struct i2c_device_id sec_pmic_id[] = {
328 { "sec_pmic", 0 },
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900329 { }
330};
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900331MODULE_DEVICE_TABLE(i2c, sec_pmic_id);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900332
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900333static struct i2c_driver sec_pmic_driver = {
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900334 .driver = {
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900335 .name = "sec_pmic",
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900336 .owner = THIS_MODULE,
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800337 .of_match_table = of_match_ptr(sec_dt_match),
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900338 },
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900339 .probe = sec_pmic_probe,
340 .remove = sec_pmic_remove,
341 .id_table = sec_pmic_id,
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900342};
343
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900344static int __init sec_pmic_init(void)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900345{
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900346 return i2c_add_driver(&sec_pmic_driver);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900347}
348
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900349subsys_initcall(sec_pmic_init);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900350
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900351static void __exit sec_pmic_exit(void)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900352{
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900353 i2c_del_driver(&sec_pmic_driver);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900354}
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900355module_exit(sec_pmic_exit);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900356
357MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
358MODULE_DESCRIPTION("Core support for the S5M MFD");
359MODULE_LICENSE("GPL");