blob: 77ee26ef594176aceb8b5d8a2518b5eb13f64479 [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>
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090028#include <linux/regmap.h>
29
Sangbeom Kim88a1cfd2012-01-20 16:09:12 +090030static struct mfd_cell s5m8751_devs[] = {
31 {
32 .name = "s5m8751-pmic",
33 }, {
34 .name = "s5m-charger",
35 }, {
36 .name = "s5m8751-codec",
37 },
38};
39
40static struct mfd_cell s5m8763_devs[] = {
41 {
42 .name = "s5m8763-pmic",
43 }, {
44 .name = "s5m-rtc",
45 }, {
46 .name = "s5m-charger",
47 },
48};
49
50static struct mfd_cell s5m8767_devs[] = {
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090051 {
52 .name = "s5m8767-pmic",
53 }, {
54 .name = "s5m-rtc",
55 },
56};
57
Sangbeom Kim9b6d1342012-07-11 21:07:55 +090058static struct mfd_cell s2mps11_devs[] = {
59 {
60 .name = "s2mps11-pmic",
61 },
62};
63
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -080064#ifdef CONFIG_OF
65static struct of_device_id sec_dt_match[] = {
66 { .compatible = "samsung,s5m8767-pmic",
67 .data = (void *)S5M8767X,
68 },
69 {},
70};
71#endif
72
Sangbeom Kim63063bf2012-07-11 21:06:55 +090073int sec_reg_read(struct sec_pmic_dev *sec_pmic, u8 reg, void *dest)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090074{
Sangbeom Kim63063bf2012-07-11 21:06:55 +090075 return regmap_read(sec_pmic->regmap, reg, dest);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090076}
Sangbeom Kim63063bf2012-07-11 21:06:55 +090077EXPORT_SYMBOL_GPL(sec_reg_read);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090078
Sangbeom Kim63063bf2012-07-11 21:06:55 +090079int sec_bulk_read(struct sec_pmic_dev *sec_pmic, u8 reg, int count, u8 *buf)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090080{
Sangbeom Kim63063bf2012-07-11 21:06:55 +090081 return regmap_bulk_read(sec_pmic->regmap, reg, buf, count);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090082}
Sangbeom Kim63063bf2012-07-11 21:06:55 +090083EXPORT_SYMBOL_GPL(sec_bulk_read);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090084
Sangbeom Kim63063bf2012-07-11 21:06:55 +090085int sec_reg_write(struct sec_pmic_dev *sec_pmic, u8 reg, u8 value)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090086{
Sangbeom Kim63063bf2012-07-11 21:06:55 +090087 return regmap_write(sec_pmic->regmap, reg, value);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090088}
Sangbeom Kim63063bf2012-07-11 21:06:55 +090089EXPORT_SYMBOL_GPL(sec_reg_write);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090090
Sangbeom Kim63063bf2012-07-11 21:06:55 +090091int sec_bulk_write(struct sec_pmic_dev *sec_pmic, u8 reg, int count, u8 *buf)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090092{
Sangbeom Kim63063bf2012-07-11 21:06:55 +090093 return regmap_raw_write(sec_pmic->regmap, reg, buf, count);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090094}
Sangbeom Kim63063bf2012-07-11 21:06:55 +090095EXPORT_SYMBOL_GPL(sec_bulk_write);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090096
Sangbeom Kim63063bf2012-07-11 21:06:55 +090097int sec_reg_update(struct sec_pmic_dev *sec_pmic, u8 reg, u8 val, u8 mask)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090098{
Sangbeom Kim63063bf2012-07-11 21:06:55 +090099 return regmap_update_bits(sec_pmic->regmap, reg, mask, val);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900100}
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900101EXPORT_SYMBOL_GPL(sec_reg_update);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900102
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900103static struct regmap_config sec_regmap_config = {
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900104 .reg_bits = 8,
105 .val_bits = 8,
106};
107
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800108
109#ifdef CONFIG_OF
110/*
111 * Only the common platform data elements for s5m8767 are parsed here from the
112 * device tree. Other sub-modules of s5m8767 such as pmic, rtc , charger and
113 * others have to parse their own platform data elements from device tree.
114 *
115 * The s5m8767 platform data structure is instantiated here and the drivers for
116 * the sub-modules need not instantiate another instance while parsing their
117 * platform data.
118 */
119static struct sec_platform_data *sec_pmic_i2c_parse_dt_pdata(
120 struct device *dev)
121{
122 struct sec_platform_data *pd;
123
124 pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
125 if (!pd) {
126 dev_err(dev, "could not allocate memory for pdata\n");
127 return ERR_PTR(-ENOMEM);
128 }
129
130 /*
131 * ToDo: the 'wakeup' member in the platform data is more of a linux
132 * specfic information. Hence, there is no binding for that yet and
133 * not parsed here.
134 */
135
136 return pd;
137}
138#else
Mark Brown197bf852013-02-04 18:31:25 +0000139static struct sec_platform_data *sec_pmic_i2c_parse_dt_pdata(
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800140 struct device *dev)
141{
142 return 0;
143}
144#endif
145
146static inline int sec_i2c_get_driver_data(struct i2c_client *i2c,
147 const struct i2c_device_id *id)
148{
149#ifdef CONFIG_OF
150 if (i2c->dev.of_node) {
151 const struct of_device_id *match;
152 match = of_match_node(sec_dt_match, i2c->dev.of_node);
153 return (int)match->data;
154 }
155#endif
156 return (int)id->driver_data;
157}
158
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900159static int sec_pmic_probe(struct i2c_client *i2c,
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900160 const struct i2c_device_id *id)
161{
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900162 struct sec_platform_data *pdata = i2c->dev.platform_data;
163 struct sec_pmic_dev *sec_pmic;
Axel Lin61a2af32012-02-28 14:23:55 +0800164 int ret;
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900165
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900166 sec_pmic = devm_kzalloc(&i2c->dev, sizeof(struct sec_pmic_dev),
Sangbeom Kim621210e2012-01-20 16:09:11 +0900167 GFP_KERNEL);
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900168 if (sec_pmic == NULL)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900169 return -ENOMEM;
170
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900171 i2c_set_clientdata(i2c, sec_pmic);
172 sec_pmic->dev = &i2c->dev;
173 sec_pmic->i2c = i2c;
174 sec_pmic->irq = i2c->irq;
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800175 sec_pmic->type = sec_i2c_get_driver_data(i2c, id);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900176
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800177 if (sec_pmic->dev->of_node) {
178 pdata = sec_pmic_i2c_parse_dt_pdata(sec_pmic->dev);
179 if (IS_ERR(pdata)) {
180 ret = PTR_ERR(pdata);
181 return ret;
182 }
183 pdata->device_type = sec_pmic->type;
184 }
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900185 if (pdata) {
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900186 sec_pmic->device_type = pdata->device_type;
187 sec_pmic->ono = pdata->ono;
188 sec_pmic->irq_base = pdata->irq_base;
189 sec_pmic->wakeup = pdata->wakeup;
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800190 sec_pmic->pdata = pdata;
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900191 }
192
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900193 sec_pmic->regmap = devm_regmap_init_i2c(i2c, &sec_regmap_config);
194 if (IS_ERR(sec_pmic->regmap)) {
195 ret = PTR_ERR(sec_pmic->regmap);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900196 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
Axel Lin61a2af32012-02-28 14:23:55 +0800197 ret);
Axel Lin1092e1c2012-04-25 10:03:44 +0800198 return ret;
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900199 }
200
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900201 sec_pmic->rtc = i2c_new_dummy(i2c->adapter, RTC_I2C_ADDR);
202 i2c_set_clientdata(sec_pmic->rtc, sec_pmic);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900203
Jonghwan Choic3ebb302012-01-16 09:08:42 +0900204 if (pdata && pdata->cfg_pmic_irq)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900205 pdata->cfg_pmic_irq();
206
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900207 sec_irq_init(sec_pmic);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900208
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900209 pm_runtime_set_active(sec_pmic->dev);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900210
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900211 switch (sec_pmic->device_type) {
Sangbeom Kim88a1cfd2012-01-20 16:09:12 +0900212 case S5M8751X:
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900213 ret = mfd_add_devices(sec_pmic->dev, -1, s5m8751_devs,
Mark Brown0848c942012-09-11 15:16:36 +0800214 ARRAY_SIZE(s5m8751_devs), NULL, 0, NULL);
Sangbeom Kim88a1cfd2012-01-20 16:09:12 +0900215 break;
216 case S5M8763X:
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900217 ret = mfd_add_devices(sec_pmic->dev, -1, s5m8763_devs,
Mark Brown0848c942012-09-11 15:16:36 +0800218 ARRAY_SIZE(s5m8763_devs), NULL, 0, NULL);
Sangbeom Kim88a1cfd2012-01-20 16:09:12 +0900219 break;
220 case S5M8767X:
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900221 ret = mfd_add_devices(sec_pmic->dev, -1, s5m8767_devs,
Mark Brown0848c942012-09-11 15:16:36 +0800222 ARRAY_SIZE(s5m8767_devs), NULL, 0, NULL);
Sangbeom Kim88a1cfd2012-01-20 16:09:12 +0900223 break;
Sangbeom Kim9b6d1342012-07-11 21:07:55 +0900224 case S2MPS11X:
225 ret = mfd_add_devices(sec_pmic->dev, -1, s2mps11_devs,
Mark Brown0848c942012-09-11 15:16:36 +0800226 ARRAY_SIZE(s2mps11_devs), NULL, 0, NULL);
Sangbeom Kim9b6d1342012-07-11 21:07:55 +0900227 break;
Sangbeom Kim88a1cfd2012-01-20 16:09:12 +0900228 default:
229 /* If this happens the probe function is problem */
230 BUG();
231 }
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900232
233 if (ret < 0)
234 goto err;
235
236 return ret;
237
238err:
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900239 mfd_remove_devices(sec_pmic->dev);
240 sec_irq_exit(sec_pmic);
241 i2c_unregister_device(sec_pmic->rtc);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900242 return ret;
243}
244
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900245static int sec_pmic_remove(struct i2c_client *i2c)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900246{
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900247 struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900248
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900249 mfd_remove_devices(sec_pmic->dev);
250 sec_irq_exit(sec_pmic);
251 i2c_unregister_device(sec_pmic->rtc);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900252 return 0;
253}
254
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900255static const struct i2c_device_id sec_pmic_id[] = {
256 { "sec_pmic", 0 },
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900257 { }
258};
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900259MODULE_DEVICE_TABLE(i2c, sec_pmic_id);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900260
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900261static struct i2c_driver sec_pmic_driver = {
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900262 .driver = {
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900263 .name = "sec_pmic",
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900264 .owner = THIS_MODULE,
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800265 .of_match_table = of_match_ptr(sec_dt_match),
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900266 },
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900267 .probe = sec_pmic_probe,
268 .remove = sec_pmic_remove,
269 .id_table = sec_pmic_id,
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900270};
271
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900272static int __init sec_pmic_init(void)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900273{
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900274 return i2c_add_driver(&sec_pmic_driver);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900275}
276
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900277subsys_initcall(sec_pmic_init);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900278
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900279static void __exit sec_pmic_exit(void)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900280{
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900281 i2c_del_driver(&sec_pmic_driver);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900282}
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900283module_exit(sec_pmic_exit);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900284
285MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
286MODULE_DESCRIPTION("Core support for the S5M MFD");
287MODULE_LICENSE("GPL");