4 * Copyright (c) 2012 Samsung Electronics Co., Ltd
5 * http://www.samsung.com
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.
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/of_irq.h>
21 #include <linux/interrupt.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/mutex.h>
24 #include <linux/mfd/core.h>
25 #include <linux/mfd/samsung/core.h>
26 #include <linux/mfd/samsung/irq.h>
27 #include <linux/mfd/samsung/rtc.h>
28 #include <linux/regmap.h>
30 static struct mfd_cell s5m8751_devs
[] = {
32 .name
= "s5m8751-pmic",
34 .name
= "s5m-charger",
36 .name
= "s5m8751-codec",
40 static struct mfd_cell s5m8763_devs
[] = {
42 .name
= "s5m8763-pmic",
46 .name
= "s5m-charger",
50 static struct mfd_cell s5m8767_devs
[] = {
52 .name
= "s5m8767-pmic",
58 static struct mfd_cell s2mps11_devs
[] = {
60 .name
= "s2mps11-pmic",
65 static struct of_device_id sec_dt_match
[] = {
66 { .compatible
= "samsung,s5m8767-pmic",
67 .data
= (void *)S5M8767X
,
73 int sec_reg_read(struct sec_pmic_dev
*sec_pmic
, u8 reg
, void *dest
)
75 return regmap_read(sec_pmic
->regmap
, reg
, dest
);
77 EXPORT_SYMBOL_GPL(sec_reg_read
);
79 int sec_bulk_read(struct sec_pmic_dev
*sec_pmic
, u8 reg
, int count
, u8
*buf
)
81 return regmap_bulk_read(sec_pmic
->regmap
, reg
, buf
, count
);
83 EXPORT_SYMBOL_GPL(sec_bulk_read
);
85 int sec_reg_write(struct sec_pmic_dev
*sec_pmic
, u8 reg
, u8 value
)
87 return regmap_write(sec_pmic
->regmap
, reg
, value
);
89 EXPORT_SYMBOL_GPL(sec_reg_write
);
91 int sec_bulk_write(struct sec_pmic_dev
*sec_pmic
, u8 reg
, int count
, u8
*buf
)
93 return regmap_raw_write(sec_pmic
->regmap
, reg
, buf
, count
);
95 EXPORT_SYMBOL_GPL(sec_bulk_write
);
97 int sec_reg_update(struct sec_pmic_dev
*sec_pmic
, u8 reg
, u8 val
, u8 mask
)
99 return regmap_update_bits(sec_pmic
->regmap
, reg
, mask
, val
);
101 EXPORT_SYMBOL_GPL(sec_reg_update
);
103 static struct regmap_config sec_regmap_config
= {
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.
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
119 static struct sec_platform_data
*sec_pmic_i2c_parse_dt_pdata(
122 struct sec_platform_data
*pd
;
124 pd
= devm_kzalloc(dev
, sizeof(*pd
), GFP_KERNEL
);
126 dev_err(dev
, "could not allocate memory for pdata\n");
127 return ERR_PTR(-ENOMEM
);
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
139 static struct sec_platform_data
*sec_pmic_i2c_parse_dt_pdata(
146 static inline int sec_i2c_get_driver_data(struct i2c_client
*i2c
,
147 const struct i2c_device_id
*id
)
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
;
156 return (int)id
->driver_data
;
159 static int sec_pmic_probe(struct i2c_client
*i2c
,
160 const struct i2c_device_id
*id
)
162 struct sec_platform_data
*pdata
= i2c
->dev
.platform_data
;
163 struct sec_pmic_dev
*sec_pmic
;
166 sec_pmic
= devm_kzalloc(&i2c
->dev
, sizeof(struct sec_pmic_dev
),
168 if (sec_pmic
== NULL
)
171 i2c_set_clientdata(i2c
, sec_pmic
);
172 sec_pmic
->dev
= &i2c
->dev
;
174 sec_pmic
->irq
= i2c
->irq
;
175 sec_pmic
->type
= sec_i2c_get_driver_data(i2c
, id
);
177 if (sec_pmic
->dev
->of_node
) {
178 pdata
= sec_pmic_i2c_parse_dt_pdata(sec_pmic
->dev
);
180 ret
= PTR_ERR(pdata
);
183 pdata
->device_type
= sec_pmic
->type
;
186 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
;
190 sec_pmic
->pdata
= pdata
;
193 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
);
196 dev_err(&i2c
->dev
, "Failed to allocate register map: %d\n",
201 sec_pmic
->rtc
= i2c_new_dummy(i2c
->adapter
, RTC_I2C_ADDR
);
202 i2c_set_clientdata(sec_pmic
->rtc
, sec_pmic
);
204 if (pdata
&& pdata
->cfg_pmic_irq
)
205 pdata
->cfg_pmic_irq();
207 sec_irq_init(sec_pmic
);
209 pm_runtime_set_active(sec_pmic
->dev
);
211 switch (sec_pmic
->device_type
) {
213 ret
= mfd_add_devices(sec_pmic
->dev
, -1, s5m8751_devs
,
214 ARRAY_SIZE(s5m8751_devs
), NULL
, 0, NULL
);
217 ret
= mfd_add_devices(sec_pmic
->dev
, -1, s5m8763_devs
,
218 ARRAY_SIZE(s5m8763_devs
), NULL
, 0, NULL
);
221 ret
= mfd_add_devices(sec_pmic
->dev
, -1, s5m8767_devs
,
222 ARRAY_SIZE(s5m8767_devs
), NULL
, 0, NULL
);
225 ret
= mfd_add_devices(sec_pmic
->dev
, -1, s2mps11_devs
,
226 ARRAY_SIZE(s2mps11_devs
), NULL
, 0, NULL
);
229 /* If this happens the probe function is problem */
239 mfd_remove_devices(sec_pmic
->dev
);
240 sec_irq_exit(sec_pmic
);
241 i2c_unregister_device(sec_pmic
->rtc
);
245 static int sec_pmic_remove(struct i2c_client
*i2c
)
247 struct sec_pmic_dev
*sec_pmic
= i2c_get_clientdata(i2c
);
249 mfd_remove_devices(sec_pmic
->dev
);
250 sec_irq_exit(sec_pmic
);
251 i2c_unregister_device(sec_pmic
->rtc
);
255 static const struct i2c_device_id sec_pmic_id
[] = {
259 MODULE_DEVICE_TABLE(i2c
, sec_pmic_id
);
261 static struct i2c_driver sec_pmic_driver
= {
264 .owner
= THIS_MODULE
,
265 .of_match_table
= of_match_ptr(sec_dt_match
),
267 .probe
= sec_pmic_probe
,
268 .remove
= sec_pmic_remove
,
269 .id_table
= sec_pmic_id
,
272 static int __init
sec_pmic_init(void)
274 return i2c_add_driver(&sec_pmic_driver
);
277 subsys_initcall(sec_pmic_init
);
279 static void __exit
sec_pmic_exit(void)
281 i2c_del_driver(&sec_pmic_driver
);
283 module_exit(sec_pmic_exit
);
285 MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
286 MODULE_DESCRIPTION("Core support for the S5M MFD");
287 MODULE_LICENSE("GPL");