blob: 2988efde11ebc60b6a714044f79d6a7cd9a44dba [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>
20#include <linux/interrupt.h>
21#include <linux/pm_runtime.h>
22#include <linux/mutex.h>
23#include <linux/mfd/core.h>
Sangbeom Kim54227bc2012-07-11 21:07:16 +090024#include <linux/mfd/samsung/core.h>
25#include <linux/mfd/samsung/irq.h>
26#include <linux/mfd/samsung/rtc.h>
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090027#include <linux/regmap.h>
28
Sangbeom Kim88a1cfd2012-01-20 16:09:12 +090029static struct mfd_cell s5m8751_devs[] = {
30 {
31 .name = "s5m8751-pmic",
32 }, {
33 .name = "s5m-charger",
34 }, {
35 .name = "s5m8751-codec",
36 },
37};
38
39static struct mfd_cell s5m8763_devs[] = {
40 {
41 .name = "s5m8763-pmic",
42 }, {
43 .name = "s5m-rtc",
44 }, {
45 .name = "s5m-charger",
46 },
47};
48
49static struct mfd_cell s5m8767_devs[] = {
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090050 {
51 .name = "s5m8767-pmic",
52 }, {
53 .name = "s5m-rtc",
54 },
55};
56
Sangbeom Kim9b6d1342012-07-11 21:07:55 +090057static struct mfd_cell s2mps11_devs[] = {
58 {
59 .name = "s2mps11-pmic",
60 },
61};
62
Sangbeom Kim63063bf2012-07-11 21:06:55 +090063int sec_reg_read(struct sec_pmic_dev *sec_pmic, u8 reg, void *dest)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090064{
Sangbeom Kim63063bf2012-07-11 21:06:55 +090065 return regmap_read(sec_pmic->regmap, reg, dest);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090066}
Sangbeom Kim63063bf2012-07-11 21:06:55 +090067EXPORT_SYMBOL_GPL(sec_reg_read);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090068
Sangbeom Kim63063bf2012-07-11 21:06:55 +090069int sec_bulk_read(struct sec_pmic_dev *sec_pmic, u8 reg, int count, u8 *buf)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090070{
Sangbeom Kim63063bf2012-07-11 21:06:55 +090071 return regmap_bulk_read(sec_pmic->regmap, reg, buf, count);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090072}
Sangbeom Kim63063bf2012-07-11 21:06:55 +090073EXPORT_SYMBOL_GPL(sec_bulk_read);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090074
Sangbeom Kim63063bf2012-07-11 21:06:55 +090075int sec_reg_write(struct sec_pmic_dev *sec_pmic, u8 reg, u8 value)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090076{
Sangbeom Kim63063bf2012-07-11 21:06:55 +090077 return regmap_write(sec_pmic->regmap, reg, value);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090078}
Sangbeom Kim63063bf2012-07-11 21:06:55 +090079EXPORT_SYMBOL_GPL(sec_reg_write);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090080
Sangbeom Kim63063bf2012-07-11 21:06:55 +090081int sec_bulk_write(struct sec_pmic_dev *sec_pmic, u8 reg, int count, u8 *buf)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090082{
Sangbeom Kim63063bf2012-07-11 21:06:55 +090083 return regmap_raw_write(sec_pmic->regmap, reg, buf, count);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090084}
Sangbeom Kim63063bf2012-07-11 21:06:55 +090085EXPORT_SYMBOL_GPL(sec_bulk_write);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090086
Sangbeom Kim63063bf2012-07-11 21:06:55 +090087int sec_reg_update(struct sec_pmic_dev *sec_pmic, u8 reg, u8 val, u8 mask)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090088{
Sangbeom Kim63063bf2012-07-11 21:06:55 +090089 return regmap_update_bits(sec_pmic->regmap, reg, mask, val);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090090}
Sangbeom Kim63063bf2012-07-11 21:06:55 +090091EXPORT_SYMBOL_GPL(sec_reg_update);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090092
Sangbeom Kim63063bf2012-07-11 21:06:55 +090093static struct regmap_config sec_regmap_config = {
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090094 .reg_bits = 8,
95 .val_bits = 8,
96};
97
Sangbeom Kim63063bf2012-07-11 21:06:55 +090098static int sec_pmic_probe(struct i2c_client *i2c,
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090099 const struct i2c_device_id *id)
100{
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900101 struct sec_platform_data *pdata = i2c->dev.platform_data;
102 struct sec_pmic_dev *sec_pmic;
Axel Lin61a2af32012-02-28 14:23:55 +0800103 int ret;
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900104
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900105 sec_pmic = devm_kzalloc(&i2c->dev, sizeof(struct sec_pmic_dev),
Sangbeom Kim621210e2012-01-20 16:09:11 +0900106 GFP_KERNEL);
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900107 if (sec_pmic == NULL)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900108 return -ENOMEM;
109
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900110 i2c_set_clientdata(i2c, sec_pmic);
111 sec_pmic->dev = &i2c->dev;
112 sec_pmic->i2c = i2c;
113 sec_pmic->irq = i2c->irq;
114 sec_pmic->type = id->driver_data;
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900115
116 if (pdata) {
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900117 sec_pmic->device_type = pdata->device_type;
118 sec_pmic->ono = pdata->ono;
119 sec_pmic->irq_base = pdata->irq_base;
120 sec_pmic->wakeup = pdata->wakeup;
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900121 }
122
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900123 sec_pmic->regmap = devm_regmap_init_i2c(i2c, &sec_regmap_config);
124 if (IS_ERR(sec_pmic->regmap)) {
125 ret = PTR_ERR(sec_pmic->regmap);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900126 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
Axel Lin61a2af32012-02-28 14:23:55 +0800127 ret);
Axel Lin1092e1c2012-04-25 10:03:44 +0800128 return ret;
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900129 }
130
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900131 sec_pmic->rtc = i2c_new_dummy(i2c->adapter, RTC_I2C_ADDR);
132 i2c_set_clientdata(sec_pmic->rtc, sec_pmic);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900133
Jonghwan Choic3ebb302012-01-16 09:08:42 +0900134 if (pdata && pdata->cfg_pmic_irq)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900135 pdata->cfg_pmic_irq();
136
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900137 sec_irq_init(sec_pmic);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900138
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900139 pm_runtime_set_active(sec_pmic->dev);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900140
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900141 switch (sec_pmic->device_type) {
Sangbeom Kim88a1cfd2012-01-20 16:09:12 +0900142 case S5M8751X:
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900143 ret = mfd_add_devices(sec_pmic->dev, -1, s5m8751_devs,
Sangbeom Kim88a1cfd2012-01-20 16:09:12 +0900144 ARRAY_SIZE(s5m8751_devs), NULL, 0);
145 break;
146 case S5M8763X:
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900147 ret = mfd_add_devices(sec_pmic->dev, -1, s5m8763_devs,
Sangbeom Kim88a1cfd2012-01-20 16:09:12 +0900148 ARRAY_SIZE(s5m8763_devs), NULL, 0);
149 break;
150 case S5M8767X:
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900151 ret = mfd_add_devices(sec_pmic->dev, -1, s5m8767_devs,
Sangbeom Kim88a1cfd2012-01-20 16:09:12 +0900152 ARRAY_SIZE(s5m8767_devs), NULL, 0);
153 break;
Sangbeom Kim9b6d1342012-07-11 21:07:55 +0900154 case S2MPS11X:
155 ret = mfd_add_devices(sec_pmic->dev, -1, s2mps11_devs,
156 ARRAY_SIZE(s2mps11_devs), NULL, 0);
157 break;
Sangbeom Kim88a1cfd2012-01-20 16:09:12 +0900158 default:
159 /* If this happens the probe function is problem */
160 BUG();
161 }
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900162
163 if (ret < 0)
164 goto err;
165
166 return ret;
167
168err:
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900169 mfd_remove_devices(sec_pmic->dev);
170 sec_irq_exit(sec_pmic);
171 i2c_unregister_device(sec_pmic->rtc);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900172 return ret;
173}
174
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900175static int sec_pmic_remove(struct i2c_client *i2c)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900176{
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900177 struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900178
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900179 mfd_remove_devices(sec_pmic->dev);
180 sec_irq_exit(sec_pmic);
181 i2c_unregister_device(sec_pmic->rtc);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900182 return 0;
183}
184
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900185static const struct i2c_device_id sec_pmic_id[] = {
186 { "sec_pmic", 0 },
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900187 { }
188};
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900189MODULE_DEVICE_TABLE(i2c, sec_pmic_id);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900190
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900191static struct i2c_driver sec_pmic_driver = {
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900192 .driver = {
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900193 .name = "sec_pmic",
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900194 .owner = THIS_MODULE,
195 },
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900196 .probe = sec_pmic_probe,
197 .remove = sec_pmic_remove,
198 .id_table = sec_pmic_id,
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900199};
200
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900201static int __init sec_pmic_init(void)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900202{
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900203 return i2c_add_driver(&sec_pmic_driver);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900204}
205
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900206subsys_initcall(sec_pmic_init);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900207
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900208static void __exit sec_pmic_exit(void)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900209{
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900210 i2c_del_driver(&sec_pmic_driver);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900211}
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900212module_exit(sec_pmic_exit);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900213
214MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
215MODULE_DESCRIPTION("Core support for the S5M MFD");
216MODULE_LICENSE("GPL");