xref: /linux/drivers/mfd/max8998.c (revision d524dac9279b6a41ffdf7ff7958c577f2e387db6)
1 /*
2  * max8998.c - mfd core driver for the Maxim 8998
3  *
4  *  Copyright (C) 2009-2010 Samsung Electronics
5  *  Kyungmin Park <kyungmin.park@samsung.com>
6  *  Marek Szyprowski <m.szyprowski@samsung.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22 
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/init.h>
26 #include <linux/slab.h>
27 #include <linux/i2c.h>
28 #include <linux/interrupt.h>
29 #include <linux/pm_runtime.h>
30 #include <linux/mutex.h>
31 #include <linux/mfd/core.h>
32 #include <linux/mfd/max8998.h>
33 #include <linux/mfd/max8998-private.h>
34 
35 #define RTC_I2C_ADDR		(0x0c >> 1)
36 
37 static struct mfd_cell max8998_devs[] = {
38 	{
39 		.name = "max8998-pmic",
40 	}, {
41 		.name = "max8998-rtc",
42 	},
43 };
44 
45 static struct mfd_cell lp3974_devs[] = {
46 	{
47 		.name = "lp3974-pmic",
48 	}, {
49 		.name = "lp3974-rtc",
50 	},
51 };
52 
53 int max8998_read_reg(struct i2c_client *i2c, u8 reg, u8 *dest)
54 {
55 	struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
56 	int ret;
57 
58 	mutex_lock(&max8998->iolock);
59 	ret = i2c_smbus_read_byte_data(i2c, reg);
60 	mutex_unlock(&max8998->iolock);
61 	if (ret < 0)
62 		return ret;
63 
64 	ret &= 0xff;
65 	*dest = ret;
66 	return 0;
67 }
68 EXPORT_SYMBOL(max8998_read_reg);
69 
70 int max8998_bulk_read(struct i2c_client *i2c, u8 reg, int count, u8 *buf)
71 {
72 	struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
73 	int ret;
74 
75 	mutex_lock(&max8998->iolock);
76 	ret = i2c_smbus_read_i2c_block_data(i2c, reg, count, buf);
77 	mutex_unlock(&max8998->iolock);
78 	if (ret < 0)
79 		return ret;
80 
81 	return 0;
82 }
83 EXPORT_SYMBOL(max8998_bulk_read);
84 
85 int max8998_write_reg(struct i2c_client *i2c, u8 reg, u8 value)
86 {
87 	struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
88 	int ret;
89 
90 	mutex_lock(&max8998->iolock);
91 	ret = i2c_smbus_write_byte_data(i2c, reg, value);
92 	mutex_unlock(&max8998->iolock);
93 	return ret;
94 }
95 EXPORT_SYMBOL(max8998_write_reg);
96 
97 int max8998_bulk_write(struct i2c_client *i2c, u8 reg, int count, u8 *buf)
98 {
99 	struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
100 	int ret;
101 
102 	mutex_lock(&max8998->iolock);
103 	ret = i2c_smbus_write_i2c_block_data(i2c, reg, count, buf);
104 	mutex_unlock(&max8998->iolock);
105 	if (ret < 0)
106 		return ret;
107 
108 	return 0;
109 }
110 EXPORT_SYMBOL(max8998_bulk_write);
111 
112 int max8998_update_reg(struct i2c_client *i2c, u8 reg, u8 val, u8 mask)
113 {
114 	struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
115 	int ret;
116 
117 	mutex_lock(&max8998->iolock);
118 	ret = i2c_smbus_read_byte_data(i2c, reg);
119 	if (ret >= 0) {
120 		u8 old_val = ret & 0xff;
121 		u8 new_val = (val & mask) | (old_val & (~mask));
122 		ret = i2c_smbus_write_byte_data(i2c, reg, new_val);
123 	}
124 	mutex_unlock(&max8998->iolock);
125 	return ret;
126 }
127 EXPORT_SYMBOL(max8998_update_reg);
128 
129 static int max8998_i2c_probe(struct i2c_client *i2c,
130 			    const struct i2c_device_id *id)
131 {
132 	struct max8998_platform_data *pdata = i2c->dev.platform_data;
133 	struct max8998_dev *max8998;
134 	int ret = 0;
135 
136 	max8998 = kzalloc(sizeof(struct max8998_dev), GFP_KERNEL);
137 	if (max8998 == NULL)
138 		return -ENOMEM;
139 
140 	i2c_set_clientdata(i2c, max8998);
141 	max8998->dev = &i2c->dev;
142 	max8998->i2c = i2c;
143 	max8998->irq = i2c->irq;
144 	max8998->type = id->driver_data;
145 	if (pdata) {
146 		max8998->ono = pdata->ono;
147 		max8998->irq_base = pdata->irq_base;
148 		max8998->wakeup = pdata->wakeup;
149 	}
150 	mutex_init(&max8998->iolock);
151 
152 	max8998->rtc = i2c_new_dummy(i2c->adapter, RTC_I2C_ADDR);
153 	i2c_set_clientdata(max8998->rtc, max8998);
154 
155 	max8998_irq_init(max8998);
156 
157 	pm_runtime_set_active(max8998->dev);
158 
159 	switch (id->driver_data) {
160 	case TYPE_LP3974:
161 		ret = mfd_add_devices(max8998->dev, -1,
162 				lp3974_devs, ARRAY_SIZE(lp3974_devs),
163 				NULL, 0);
164 		break;
165 	case TYPE_MAX8998:
166 		ret = mfd_add_devices(max8998->dev, -1,
167 				max8998_devs, ARRAY_SIZE(max8998_devs),
168 				NULL, 0);
169 		break;
170 	default:
171 		ret = -EINVAL;
172 	}
173 
174 	if (ret < 0)
175 		goto err;
176 
177 	return ret;
178 
179 err:
180 	mfd_remove_devices(max8998->dev);
181 	max8998_irq_exit(max8998);
182 	i2c_unregister_device(max8998->rtc);
183 	kfree(max8998);
184 	return ret;
185 }
186 
187 static int max8998_i2c_remove(struct i2c_client *i2c)
188 {
189 	struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
190 
191 	mfd_remove_devices(max8998->dev);
192 	max8998_irq_exit(max8998);
193 	i2c_unregister_device(max8998->rtc);
194 	kfree(max8998);
195 
196 	return 0;
197 }
198 
199 static const struct i2c_device_id max8998_i2c_id[] = {
200 	{ "max8998", TYPE_MAX8998 },
201 	{ "lp3974", TYPE_LP3974},
202 	{ }
203 };
204 MODULE_DEVICE_TABLE(i2c, max8998_i2c_id);
205 
206 static int max8998_suspend(struct device *dev)
207 {
208 	struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
209 	struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
210 
211 	if (max8998->wakeup)
212 		set_irq_wake(max8998->irq, 1);
213 	return 0;
214 }
215 
216 static int max8998_resume(struct device *dev)
217 {
218 	struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
219 	struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
220 
221 	if (max8998->wakeup)
222 		set_irq_wake(max8998->irq, 0);
223 	/*
224 	 * In LP3974, if IRQ registers are not "read & clear"
225 	 * when it's set during sleep, the interrupt becomes
226 	 * disabled.
227 	 */
228 	return max8998_irq_resume(i2c_get_clientdata(i2c));
229 }
230 
231 struct max8998_reg_dump {
232 	u8	addr;
233 	u8	val;
234 };
235 #define SAVE_ITEM(x)	{ .addr = (x), .val = 0x0, }
236 struct max8998_reg_dump max8998_dump[] = {
237 	SAVE_ITEM(MAX8998_REG_IRQM1),
238 	SAVE_ITEM(MAX8998_REG_IRQM2),
239 	SAVE_ITEM(MAX8998_REG_IRQM3),
240 	SAVE_ITEM(MAX8998_REG_IRQM4),
241 	SAVE_ITEM(MAX8998_REG_STATUSM1),
242 	SAVE_ITEM(MAX8998_REG_STATUSM2),
243 	SAVE_ITEM(MAX8998_REG_CHGR1),
244 	SAVE_ITEM(MAX8998_REG_CHGR2),
245 	SAVE_ITEM(MAX8998_REG_LDO_ACTIVE_DISCHARGE1),
246 	SAVE_ITEM(MAX8998_REG_LDO_ACTIVE_DISCHARGE1),
247 	SAVE_ITEM(MAX8998_REG_BUCK_ACTIVE_DISCHARGE3),
248 	SAVE_ITEM(MAX8998_REG_ONOFF1),
249 	SAVE_ITEM(MAX8998_REG_ONOFF2),
250 	SAVE_ITEM(MAX8998_REG_ONOFF3),
251 	SAVE_ITEM(MAX8998_REG_ONOFF4),
252 	SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE1),
253 	SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE2),
254 	SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE3),
255 	SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE4),
256 	SAVE_ITEM(MAX8998_REG_BUCK2_VOLTAGE1),
257 	SAVE_ITEM(MAX8998_REG_BUCK2_VOLTAGE2),
258 	SAVE_ITEM(MAX8998_REG_LDO2_LDO3),
259 	SAVE_ITEM(MAX8998_REG_LDO4),
260 	SAVE_ITEM(MAX8998_REG_LDO5),
261 	SAVE_ITEM(MAX8998_REG_LDO6),
262 	SAVE_ITEM(MAX8998_REG_LDO7),
263 	SAVE_ITEM(MAX8998_REG_LDO8_LDO9),
264 	SAVE_ITEM(MAX8998_REG_LDO10_LDO11),
265 	SAVE_ITEM(MAX8998_REG_LDO12),
266 	SAVE_ITEM(MAX8998_REG_LDO13),
267 	SAVE_ITEM(MAX8998_REG_LDO14),
268 	SAVE_ITEM(MAX8998_REG_LDO15),
269 	SAVE_ITEM(MAX8998_REG_LDO16),
270 	SAVE_ITEM(MAX8998_REG_LDO17),
271 	SAVE_ITEM(MAX8998_REG_BKCHR),
272 	SAVE_ITEM(MAX8998_REG_LBCNFG1),
273 	SAVE_ITEM(MAX8998_REG_LBCNFG2),
274 };
275 /* Save registers before hibernation */
276 static int max8998_freeze(struct device *dev)
277 {
278 	struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
279 	int i;
280 
281 	for (i = 0; i < ARRAY_SIZE(max8998_dump); i++)
282 		max8998_read_reg(i2c, max8998_dump[i].addr,
283 				&max8998_dump[i].val);
284 
285 	return 0;
286 }
287 
288 /* Restore registers after hibernation */
289 static int max8998_restore(struct device *dev)
290 {
291 	struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
292 	int i;
293 
294 	for (i = 0; i < ARRAY_SIZE(max8998_dump); i++)
295 		max8998_write_reg(i2c, max8998_dump[i].addr,
296 				max8998_dump[i].val);
297 
298 	return 0;
299 }
300 
301 const struct dev_pm_ops max8998_pm = {
302 	.suspend = max8998_suspend,
303 	.resume = max8998_resume,
304 	.freeze = max8998_freeze,
305 	.restore = max8998_restore,
306 };
307 
308 static struct i2c_driver max8998_i2c_driver = {
309 	.driver = {
310 		   .name = "max8998",
311 		   .owner = THIS_MODULE,
312 		   .pm = &max8998_pm,
313 	},
314 	.probe = max8998_i2c_probe,
315 	.remove = max8998_i2c_remove,
316 	.id_table = max8998_i2c_id,
317 };
318 
319 static int __init max8998_i2c_init(void)
320 {
321 	return i2c_add_driver(&max8998_i2c_driver);
322 }
323 /* init early so consumer devices can complete system boot */
324 subsys_initcall(max8998_i2c_init);
325 
326 static void __exit max8998_i2c_exit(void)
327 {
328 	i2c_del_driver(&max8998_i2c_driver);
329 }
330 module_exit(max8998_i2c_exit);
331 
332 MODULE_DESCRIPTION("MAXIM 8998 multi-function core driver");
333 MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
334 MODULE_LICENSE("GPL");
335