blob: 75853988fe3e28ef2fbca25d84c39220f443fa68 [file] [log] [blame]
Sangbeom Kim0f5f7072011-12-23 17:28:08 +09001/*
2 * s5m87xx.c
3 *
4 * Copyright (c) 2011 Samsung Electronics Co., Ltd
5 * 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>
24#include <linux/mfd/s5m87xx/s5m-core.h>
25#include <linux/mfd/s5m87xx/s5m-pmic.h>
26#include <linux/mfd/s5m87xx/s5m-rtc.h>
27#include <linux/regmap.h>
28
29static struct mfd_cell s5m87xx_devs[] = {
30 {
31 .name = "s5m8767-pmic",
32 }, {
33 .name = "s5m-rtc",
34 },
35};
36
37int s5m_reg_read(struct s5m87xx_dev *s5m87xx, u8 reg, void *dest)
38{
39 return regmap_read(s5m87xx->regmap, reg, dest);
40}
41EXPORT_SYMBOL_GPL(s5m_reg_read);
42
43int s5m_bulk_read(struct s5m87xx_dev *s5m87xx, u8 reg, int count, u8 *buf)
44{
45 return regmap_bulk_read(s5m87xx->regmap, reg, buf, count);;
46}
47EXPORT_SYMBOL_GPL(s5m_bulk_read);
48
49int s5m_reg_write(struct s5m87xx_dev *s5m87xx, u8 reg, u8 value)
50{
51 return regmap_write(s5m87xx->regmap, reg, value);
52}
53EXPORT_SYMBOL_GPL(s5m_reg_write);
54
55int s5m_bulk_write(struct s5m87xx_dev *s5m87xx, u8 reg, int count, u8 *buf)
56{
57 return regmap_raw_write(s5m87xx->regmap, reg, buf, count * sizeof(u16));
58}
59EXPORT_SYMBOL_GPL(s5m_bulk_write);
60
61int s5m_reg_update(struct s5m87xx_dev *s5m87xx, u8 reg, u8 val, u8 mask)
62{
63 return regmap_update_bits(s5m87xx->regmap, reg, mask, val);
64}
65EXPORT_SYMBOL_GPL(s5m_reg_update);
66
67static struct regmap_config s5m_regmap_config = {
68 .reg_bits = 8,
69 .val_bits = 8,
70};
71
72static int s5m87xx_i2c_probe(struct i2c_client *i2c,
73 const struct i2c_device_id *id)
74{
75 struct s5m_platform_data *pdata = i2c->dev.platform_data;
76 struct s5m87xx_dev *s5m87xx;
77 int ret = 0;
78 int error;
79
Sangbeom Kim621210e2012-01-20 16:09:11 +090080 s5m87xx = devm_kzalloc(&i2c->dev, sizeof(struct s5m87xx_dev),
81 GFP_KERNEL);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +090082 if (s5m87xx == NULL)
83 return -ENOMEM;
84
85 i2c_set_clientdata(i2c, s5m87xx);
86 s5m87xx->dev = &i2c->dev;
87 s5m87xx->i2c = i2c;
88 s5m87xx->irq = i2c->irq;
89 s5m87xx->type = id->driver_data;
90
91 if (pdata) {
92 s5m87xx->device_type = pdata->device_type;
93 s5m87xx->ono = pdata->ono;
94 s5m87xx->irq_base = pdata->irq_base;
95 s5m87xx->wakeup = pdata->wakeup;
96 }
97
98 s5m87xx->regmap = regmap_init_i2c(i2c, &s5m_regmap_config);
99 if (IS_ERR(s5m87xx->regmap)) {
100 error = PTR_ERR(s5m87xx->regmap);
101 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
102 error);
103 goto err;
104 }
105
106 s5m87xx->rtc = i2c_new_dummy(i2c->adapter, RTC_I2C_ADDR);
107 i2c_set_clientdata(s5m87xx->rtc, s5m87xx);
108
Jonghwan Choic3ebb302012-01-16 09:08:42 +0900109 if (pdata && pdata->cfg_pmic_irq)
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900110 pdata->cfg_pmic_irq();
111
112 s5m_irq_init(s5m87xx);
113
114 pm_runtime_set_active(s5m87xx->dev);
115
116 ret = mfd_add_devices(s5m87xx->dev, -1,
117 s5m87xx_devs, ARRAY_SIZE(s5m87xx_devs),
118 NULL, 0);
119
120 if (ret < 0)
121 goto err;
122
123 return ret;
124
125err:
126 mfd_remove_devices(s5m87xx->dev);
127 s5m_irq_exit(s5m87xx);
128 i2c_unregister_device(s5m87xx->rtc);
129 regmap_exit(s5m87xx->regmap);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900130 return ret;
131}
132
133static int s5m87xx_i2c_remove(struct i2c_client *i2c)
134{
135 struct s5m87xx_dev *s5m87xx = i2c_get_clientdata(i2c);
136
137 mfd_remove_devices(s5m87xx->dev);
138 s5m_irq_exit(s5m87xx);
139 i2c_unregister_device(s5m87xx->rtc);
140 regmap_exit(s5m87xx->regmap);
Sangbeom Kim0f5f7072011-12-23 17:28:08 +0900141 return 0;
142}
143
144static const struct i2c_device_id s5m87xx_i2c_id[] = {
145 { "s5m87xx", 0 },
146 { }
147};
148MODULE_DEVICE_TABLE(i2c, s5m87xx_i2c_id);
149
150static struct i2c_driver s5m87xx_i2c_driver = {
151 .driver = {
152 .name = "s5m87xx",
153 .owner = THIS_MODULE,
154 },
155 .probe = s5m87xx_i2c_probe,
156 .remove = s5m87xx_i2c_remove,
157 .id_table = s5m87xx_i2c_id,
158};
159
160static int __init s5m87xx_i2c_init(void)
161{
162 return i2c_add_driver(&s5m87xx_i2c_driver);
163}
164
165subsys_initcall(s5m87xx_i2c_init);
166
167static void __exit s5m87xx_i2c_exit(void)
168{
169 i2c_del_driver(&s5m87xx_i2c_driver);
170}
171module_exit(s5m87xx_i2c_exit);
172
173MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
174MODULE_DESCRIPTION("Core support for the S5M MFD");
175MODULE_LICENSE("GPL");