blob: f530e4b73f19abd63fe42ba7f18230dba79c088f [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",
Yadwinder Singh Brar3134bca2013-07-07 17:14:21 +053064 }, {
65 .name = "s2mps11-clk",
66 }
Sangbeom Kim9b6d1342012-07-11 21:07:55 +090067};
68
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -080069#ifdef CONFIG_OF
70static struct of_device_id sec_dt_match[] = {
71 { .compatible = "samsung,s5m8767-pmic",
72 .data = (void *)S5M8767X,
73 },
Yadwinder Singh Braraa32aca2013-06-29 18:21:20 +053074 { .compatible = "samsung,s2mps11-pmic",
75 .data = (void *)S2MPS11X,
76 },
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -080077 {},
78};
79#endif
80
Sangbeom Kim63063bf2012-07-11 21:06:55 +090081int sec_reg_read(struct sec_pmic_dev *sec_pmic, u8 reg, void *dest)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090082{
Sangbeom Kim63063bf2012-07-11 21:06:55 +090083 return regmap_read(sec_pmic->regmap, reg, dest);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090084}
Sangbeom Kim63063bf2012-07-11 21:06:55 +090085EXPORT_SYMBOL_GPL(sec_reg_read);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090086
Sangbeom Kim63063bf2012-07-11 21:06:55 +090087int sec_bulk_read(struct sec_pmic_dev *sec_pmic, u8 reg, int count, u8 *buf)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090088{
Sangbeom Kim63063bf2012-07-11 21:06:55 +090089 return regmap_bulk_read(sec_pmic->regmap, reg, buf, count);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090090}
Sangbeom Kim63063bf2012-07-11 21:06:55 +090091EXPORT_SYMBOL_GPL(sec_bulk_read);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090092
Sangbeom Kim63063bf2012-07-11 21:06:55 +090093int sec_reg_write(struct sec_pmic_dev *sec_pmic, u8 reg, u8 value)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090094{
Sangbeom Kim63063bf2012-07-11 21:06:55 +090095 return regmap_write(sec_pmic->regmap, reg, value);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090096}
Sangbeom Kim63063bf2012-07-11 21:06:55 +090097EXPORT_SYMBOL_GPL(sec_reg_write);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090098
Sangbeom Kim63063bf2012-07-11 21:06:55 +090099int sec_bulk_write(struct sec_pmic_dev *sec_pmic, u8 reg, int count, u8 *buf)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900100{
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900101 return regmap_raw_write(sec_pmic->regmap, reg, buf, count);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900102}
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900103EXPORT_SYMBOL_GPL(sec_bulk_write);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900104
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900105int sec_reg_update(struct sec_pmic_dev *sec_pmic, u8 reg, u8 val, u8 mask)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900106{
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900107 return regmap_update_bits(sec_pmic->regmap, reg, mask, val);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900108}
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900109EXPORT_SYMBOL_GPL(sec_reg_update);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900110
Mark Brown6b845ba2013-07-02 18:51:16 +0100111static bool s2mps11_volatile(struct device *dev, unsigned int reg)
112{
113 switch (reg) {
114 case S2MPS11_REG_INT1M:
115 case S2MPS11_REG_INT2M:
116 case S2MPS11_REG_INT3M:
117 return false;
118 default:
119 return true;
120 }
121}
122
123static bool s5m8763_volatile(struct device *dev, unsigned int reg)
124{
125 switch (reg) {
126 case S5M8763_REG_IRQM1:
127 case S5M8763_REG_IRQM2:
128 case S5M8763_REG_IRQM3:
129 case S5M8763_REG_IRQM4:
130 return false;
131 default:
132 return true;
133 }
134}
135
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900136static struct regmap_config sec_regmap_config = {
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900137 .reg_bits = 8,
138 .val_bits = 8,
139};
140
Mark Brown25f311f2013-07-01 23:02:19 +0100141static struct regmap_config s2mps11_regmap_config = {
142 .reg_bits = 8,
143 .val_bits = 8,
144
145 .max_register = S2MPS11_REG_L38CTRL,
Mark Brown6b845ba2013-07-02 18:51:16 +0100146 .volatile_reg = s2mps11_volatile,
147 .cache_type = REGCACHE_FLAT,
Mark Brown25f311f2013-07-01 23:02:19 +0100148};
149
150static struct regmap_config s5m8763_regmap_config = {
151 .reg_bits = 8,
152 .val_bits = 8,
153
154 .max_register = S5M8763_REG_LBCNFG2,
Mark Brown6b845ba2013-07-02 18:51:16 +0100155 .volatile_reg = s5m8763_volatile,
156 .cache_type = REGCACHE_FLAT,
Mark Brown25f311f2013-07-01 23:02:19 +0100157};
158
159static struct regmap_config s5m8767_regmap_config = {
160 .reg_bits = 8,
161 .val_bits = 8,
162
163 .max_register = S5M8767_REG_LDO28CTRL,
Mark Brown6b845ba2013-07-02 18:51:16 +0100164 .volatile_reg = s2mps11_volatile,
165 .cache_type = REGCACHE_FLAT,
Mark Brown25f311f2013-07-01 23:02:19 +0100166};
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800167
168#ifdef CONFIG_OF
169/*
170 * Only the common platform data elements for s5m8767 are parsed here from the
171 * device tree. Other sub-modules of s5m8767 such as pmic, rtc , charger and
172 * others have to parse their own platform data elements from device tree.
173 *
174 * The s5m8767 platform data structure is instantiated here and the drivers for
175 * the sub-modules need not instantiate another instance while parsing their
176 * platform data.
177 */
178static struct sec_platform_data *sec_pmic_i2c_parse_dt_pdata(
179 struct device *dev)
180{
181 struct sec_platform_data *pd;
182
183 pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
184 if (!pd) {
185 dev_err(dev, "could not allocate memory for pdata\n");
186 return ERR_PTR(-ENOMEM);
187 }
188
189 /*
190 * ToDo: the 'wakeup' member in the platform data is more of a linux
191 * specfic information. Hence, there is no binding for that yet and
192 * not parsed here.
193 */
194
195 return pd;
196}
197#else
Mark Brown197bf852013-02-04 18:31:25 +0000198static struct sec_platform_data *sec_pmic_i2c_parse_dt_pdata(
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800199 struct device *dev)
200{
201 return 0;
202}
203#endif
204
205static inline int sec_i2c_get_driver_data(struct i2c_client *i2c,
206 const struct i2c_device_id *id)
207{
208#ifdef CONFIG_OF
209 if (i2c->dev.of_node) {
210 const struct of_device_id *match;
211 match = of_match_node(sec_dt_match, i2c->dev.of_node);
212 return (int)match->data;
213 }
214#endif
215 return (int)id->driver_data;
216}
217
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900218static int sec_pmic_probe(struct i2c_client *i2c,
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900219 const struct i2c_device_id *id)
220{
Jingoo Han334a41c2013-07-30 17:10:05 +0900221 struct sec_platform_data *pdata = dev_get_platdata(&i2c->dev);
Mark Brown25f311f2013-07-01 23:02:19 +0100222 const struct regmap_config *regmap;
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900223 struct sec_pmic_dev *sec_pmic;
Axel Lin61a2af32012-02-28 14:23:55 +0800224 int ret;
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900225
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900226 sec_pmic = devm_kzalloc(&i2c->dev, sizeof(struct sec_pmic_dev),
Sangbeom Kim621210e2012-01-20 16:09:11 +0900227 GFP_KERNEL);
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900228 if (sec_pmic == NULL)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900229 return -ENOMEM;
230
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900231 i2c_set_clientdata(i2c, sec_pmic);
232 sec_pmic->dev = &i2c->dev;
233 sec_pmic->i2c = i2c;
234 sec_pmic->irq = i2c->irq;
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800235 sec_pmic->type = sec_i2c_get_driver_data(i2c, id);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900236
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800237 if (sec_pmic->dev->of_node) {
238 pdata = sec_pmic_i2c_parse_dt_pdata(sec_pmic->dev);
239 if (IS_ERR(pdata)) {
240 ret = PTR_ERR(pdata);
241 return ret;
242 }
243 pdata->device_type = sec_pmic->type;
244 }
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900245 if (pdata) {
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900246 sec_pmic->device_type = pdata->device_type;
247 sec_pmic->ono = pdata->ono;
248 sec_pmic->irq_base = pdata->irq_base;
249 sec_pmic->wakeup = pdata->wakeup;
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800250 sec_pmic->pdata = pdata;
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900251 }
252
Mark Brown25f311f2013-07-01 23:02:19 +0100253 switch (sec_pmic->device_type) {
254 case S2MPS11X:
255 regmap = &s2mps11_regmap_config;
256 break;
257 case S5M8763X:
258 regmap = &s5m8763_regmap_config;
259 break;
260 case S5M8767X:
261 regmap = &s5m8767_regmap_config;
262 break;
263 default:
264 regmap = &sec_regmap_config;
265 break;
266 }
267
268 sec_pmic->regmap = devm_regmap_init_i2c(i2c, regmap);
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900269 if (IS_ERR(sec_pmic->regmap)) {
270 ret = PTR_ERR(sec_pmic->regmap);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900271 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
Axel Lin61a2af32012-02-28 14:23:55 +0800272 ret);
Axel Lin1092e1c2012-04-25 10:03:44 +0800273 return ret;
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900274 }
275
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900276 sec_pmic->rtc = i2c_new_dummy(i2c->adapter, RTC_I2C_ADDR);
277 i2c_set_clientdata(sec_pmic->rtc, sec_pmic);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900278
Jonghwan Choic3ebb302012-01-16 09:08:42 +0900279 if (pdata && pdata->cfg_pmic_irq)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900280 pdata->cfg_pmic_irq();
281
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900282 sec_irq_init(sec_pmic);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900283
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900284 pm_runtime_set_active(sec_pmic->dev);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900285
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900286 switch (sec_pmic->device_type) {
Sangbeom Kim88a1cfd2012-01-20 16:09:12 +0900287 case S5M8751X:
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900288 ret = mfd_add_devices(sec_pmic->dev, -1, s5m8751_devs,
Mark Brown0848c942012-09-11 15:16:36 +0800289 ARRAY_SIZE(s5m8751_devs), NULL, 0, NULL);
Sangbeom Kim88a1cfd2012-01-20 16:09:12 +0900290 break;
291 case S5M8763X:
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900292 ret = mfd_add_devices(sec_pmic->dev, -1, s5m8763_devs,
Mark Brown0848c942012-09-11 15:16:36 +0800293 ARRAY_SIZE(s5m8763_devs), NULL, 0, NULL);
Sangbeom Kim88a1cfd2012-01-20 16:09:12 +0900294 break;
295 case S5M8767X:
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900296 ret = mfd_add_devices(sec_pmic->dev, -1, s5m8767_devs,
Mark Brown0848c942012-09-11 15:16:36 +0800297 ARRAY_SIZE(s5m8767_devs), NULL, 0, NULL);
Sangbeom Kim88a1cfd2012-01-20 16:09:12 +0900298 break;
Sangbeom Kim9b6d1342012-07-11 21:07:55 +0900299 case S2MPS11X:
300 ret = mfd_add_devices(sec_pmic->dev, -1, s2mps11_devs,
Mark Brown0848c942012-09-11 15:16:36 +0800301 ARRAY_SIZE(s2mps11_devs), NULL, 0, NULL);
Sangbeom Kim9b6d1342012-07-11 21:07:55 +0900302 break;
Sangbeom Kim88a1cfd2012-01-20 16:09:12 +0900303 default:
304 /* If this happens the probe function is problem */
305 BUG();
306 }
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900307
Leon Romanovsky15447a462013-05-15 18:00:48 +0300308 if (ret)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900309 goto err;
310
311 return ret;
312
313err:
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900314 sec_irq_exit(sec_pmic);
315 i2c_unregister_device(sec_pmic->rtc);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900316 return ret;
317}
318
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900319static int sec_pmic_remove(struct i2c_client *i2c)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900320{
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900321 struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900322
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900323 mfd_remove_devices(sec_pmic->dev);
324 sec_irq_exit(sec_pmic);
325 i2c_unregister_device(sec_pmic->rtc);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900326 return 0;
327}
328
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900329static const struct i2c_device_id sec_pmic_id[] = {
330 { "sec_pmic", 0 },
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900331 { }
332};
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900333MODULE_DEVICE_TABLE(i2c, sec_pmic_id);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900334
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900335static struct i2c_driver sec_pmic_driver = {
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900336 .driver = {
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900337 .name = "sec_pmic",
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900338 .owner = THIS_MODULE,
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800339 .of_match_table = of_match_ptr(sec_dt_match),
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900340 },
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900341 .probe = sec_pmic_probe,
342 .remove = sec_pmic_remove,
343 .id_table = sec_pmic_id,
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900344};
345
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900346static int __init sec_pmic_init(void)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900347{
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900348 return i2c_add_driver(&sec_pmic_driver);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900349}
350
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900351subsys_initcall(sec_pmic_init);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900352
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900353static void __exit sec_pmic_exit(void)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900354{
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900355 i2c_del_driver(&sec_pmic_driver);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900356}
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900357module_exit(sec_pmic_exit);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900358
359MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
360MODULE_DESCRIPTION("Core support for the S5M MFD");
361MODULE_LICENSE("GPL");