1 /*
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 
29 static struct mfd_cell s5m87xx_devs[] = {
30 	{
31 		.name = "s5m8767-pmic",
32 	}, {
33 		.name = "s5m-rtc",
34 	},
35 };
36 
s5m_reg_read(struct s5m87xx_dev * s5m87xx,u8 reg,void * dest)37 int s5m_reg_read(struct s5m87xx_dev *s5m87xx, u8 reg, void *dest)
38 {
39 	return regmap_read(s5m87xx->regmap, reg, dest);
40 }
41 EXPORT_SYMBOL_GPL(s5m_reg_read);
42 
s5m_bulk_read(struct s5m87xx_dev * s5m87xx,u8 reg,int count,u8 * buf)43 int 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 }
47 EXPORT_SYMBOL_GPL(s5m_bulk_read);
48 
s5m_reg_write(struct s5m87xx_dev * s5m87xx,u8 reg,u8 value)49 int s5m_reg_write(struct s5m87xx_dev *s5m87xx, u8 reg, u8 value)
50 {
51 	return regmap_write(s5m87xx->regmap, reg, value);
52 }
53 EXPORT_SYMBOL_GPL(s5m_reg_write);
54 
s5m_bulk_write(struct s5m87xx_dev * s5m87xx,u8 reg,int count,u8 * buf)55 int 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 }
59 EXPORT_SYMBOL_GPL(s5m_bulk_write);
60 
s5m_reg_update(struct s5m87xx_dev * s5m87xx,u8 reg,u8 val,u8 mask)61 int 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 }
65 EXPORT_SYMBOL_GPL(s5m_reg_update);
66 
67 static struct regmap_config s5m_regmap_config = {
68 	.reg_bits = 8,
69 	.val_bits = 8,
70 };
71 
s5m87xx_i2c_probe(struct i2c_client * i2c,const struct i2c_device_id * id)72 static 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 
80 	s5m87xx = kzalloc(sizeof(struct s5m87xx_dev), GFP_KERNEL);
81 	if (s5m87xx == NULL)
82 		return -ENOMEM;
83 
84 	i2c_set_clientdata(i2c, s5m87xx);
85 	s5m87xx->dev = &i2c->dev;
86 	s5m87xx->i2c = i2c;
87 	s5m87xx->irq = i2c->irq;
88 	s5m87xx->type = id->driver_data;
89 
90 	if (pdata) {
91 		s5m87xx->device_type = pdata->device_type;
92 		s5m87xx->ono = pdata->ono;
93 		s5m87xx->irq_base = pdata->irq_base;
94 		s5m87xx->wakeup = pdata->wakeup;
95 	}
96 
97 	s5m87xx->regmap = regmap_init_i2c(i2c, &s5m_regmap_config);
98 	if (IS_ERR(s5m87xx->regmap)) {
99 		error = PTR_ERR(s5m87xx->regmap);
100 		dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
101 			error);
102 		goto err;
103 	}
104 
105 	s5m87xx->rtc = i2c_new_dummy(i2c->adapter, RTC_I2C_ADDR);
106 	i2c_set_clientdata(s5m87xx->rtc, s5m87xx);
107 
108 	if (pdata && pdata->cfg_pmic_irq)
109 		pdata->cfg_pmic_irq();
110 
111 	s5m_irq_init(s5m87xx);
112 
113 	pm_runtime_set_active(s5m87xx->dev);
114 
115 	ret = mfd_add_devices(s5m87xx->dev, -1,
116 				s5m87xx_devs, ARRAY_SIZE(s5m87xx_devs),
117 				NULL, 0);
118 
119 	if (ret < 0)
120 		goto err;
121 
122 	return ret;
123 
124 err:
125 	mfd_remove_devices(s5m87xx->dev);
126 	s5m_irq_exit(s5m87xx);
127 	i2c_unregister_device(s5m87xx->rtc);
128 	regmap_exit(s5m87xx->regmap);
129 	kfree(s5m87xx);
130 	return ret;
131 }
132 
s5m87xx_i2c_remove(struct i2c_client * i2c)133 static 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);
141 	kfree(s5m87xx);
142 	return 0;
143 }
144 
145 static const struct i2c_device_id s5m87xx_i2c_id[] = {
146 	{ "s5m87xx", 0 },
147 	{ }
148 };
149 MODULE_DEVICE_TABLE(i2c, s5m87xx_i2c_id);
150 
151 static struct i2c_driver s5m87xx_i2c_driver = {
152 	.driver = {
153 		   .name = "s5m87xx",
154 		   .owner = THIS_MODULE,
155 	},
156 	.probe = s5m87xx_i2c_probe,
157 	.remove = s5m87xx_i2c_remove,
158 	.id_table = s5m87xx_i2c_id,
159 };
160 
s5m87xx_i2c_init(void)161 static int __init s5m87xx_i2c_init(void)
162 {
163 	return i2c_add_driver(&s5m87xx_i2c_driver);
164 }
165 
166 subsys_initcall(s5m87xx_i2c_init);
167 
s5m87xx_i2c_exit(void)168 static void __exit s5m87xx_i2c_exit(void)
169 {
170 	i2c_del_driver(&s5m87xx_i2c_driver);
171 }
172 module_exit(s5m87xx_i2c_exit);
173 
174 MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
175 MODULE_DESCRIPTION("Core support for the S5M MFD");
176 MODULE_LICENSE("GPL");
177