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 .name = "max8998-battery",
44 },
45 };
46
47 static struct mfd_cell lp3974_devs[] = {
48 {
49 .name = "lp3974-pmic",
50 }, {
51 .name = "lp3974-rtc",
52 },
53 };
54
max8998_read_reg(struct i2c_client * i2c,u8 reg,u8 * dest)55 int max8998_read_reg(struct i2c_client *i2c, u8 reg, u8 *dest)
56 {
57 struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
58 int ret;
59
60 mutex_lock(&max8998->iolock);
61 ret = i2c_smbus_read_byte_data(i2c, reg);
62 mutex_unlock(&max8998->iolock);
63 if (ret < 0)
64 return ret;
65
66 ret &= 0xff;
67 *dest = ret;
68 return 0;
69 }
70 EXPORT_SYMBOL(max8998_read_reg);
71
max8998_bulk_read(struct i2c_client * i2c,u8 reg,int count,u8 * buf)72 int max8998_bulk_read(struct i2c_client *i2c, u8 reg, int count, u8 *buf)
73 {
74 struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
75 int ret;
76
77 mutex_lock(&max8998->iolock);
78 ret = i2c_smbus_read_i2c_block_data(i2c, reg, count, buf);
79 mutex_unlock(&max8998->iolock);
80 if (ret < 0)
81 return ret;
82
83 return 0;
84 }
85 EXPORT_SYMBOL(max8998_bulk_read);
86
max8998_write_reg(struct i2c_client * i2c,u8 reg,u8 value)87 int max8998_write_reg(struct i2c_client *i2c, u8 reg, u8 value)
88 {
89 struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
90 int ret;
91
92 mutex_lock(&max8998->iolock);
93 ret = i2c_smbus_write_byte_data(i2c, reg, value);
94 mutex_unlock(&max8998->iolock);
95 return ret;
96 }
97 EXPORT_SYMBOL(max8998_write_reg);
98
max8998_bulk_write(struct i2c_client * i2c,u8 reg,int count,u8 * buf)99 int max8998_bulk_write(struct i2c_client *i2c, u8 reg, int count, u8 *buf)
100 {
101 struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
102 int ret;
103
104 mutex_lock(&max8998->iolock);
105 ret = i2c_smbus_write_i2c_block_data(i2c, reg, count, buf);
106 mutex_unlock(&max8998->iolock);
107 if (ret < 0)
108 return ret;
109
110 return 0;
111 }
112 EXPORT_SYMBOL(max8998_bulk_write);
113
max8998_update_reg(struct i2c_client * i2c,u8 reg,u8 val,u8 mask)114 int max8998_update_reg(struct i2c_client *i2c, u8 reg, u8 val, u8 mask)
115 {
116 struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
117 int ret;
118
119 mutex_lock(&max8998->iolock);
120 ret = i2c_smbus_read_byte_data(i2c, reg);
121 if (ret >= 0) {
122 u8 old_val = ret & 0xff;
123 u8 new_val = (val & mask) | (old_val & (~mask));
124 ret = i2c_smbus_write_byte_data(i2c, reg, new_val);
125 }
126 mutex_unlock(&max8998->iolock);
127 return ret;
128 }
129 EXPORT_SYMBOL(max8998_update_reg);
130
max8998_i2c_probe(struct i2c_client * i2c,const struct i2c_device_id * id)131 static int max8998_i2c_probe(struct i2c_client *i2c,
132 const struct i2c_device_id *id)
133 {
134 struct max8998_platform_data *pdata = i2c->dev.platform_data;
135 struct max8998_dev *max8998;
136 int ret = 0;
137
138 max8998 = kzalloc(sizeof(struct max8998_dev), GFP_KERNEL);
139 if (max8998 == NULL)
140 return -ENOMEM;
141
142 i2c_set_clientdata(i2c, max8998);
143 max8998->dev = &i2c->dev;
144 max8998->i2c = i2c;
145 max8998->irq = i2c->irq;
146 max8998->type = id->driver_data;
147 if (pdata) {
148 max8998->ono = pdata->ono;
149 max8998->irq_base = pdata->irq_base;
150 max8998->wakeup = pdata->wakeup;
151 }
152 mutex_init(&max8998->iolock);
153
154 max8998->rtc = i2c_new_dummy(i2c->adapter, RTC_I2C_ADDR);
155 if (!max8998->rtc) {
156 dev_err(&i2c->dev, "Failed to allocate I2C device for RTC\n");
157 return -ENODEV;
158 }
159 i2c_set_clientdata(max8998->rtc, max8998);
160
161 max8998_irq_init(max8998);
162
163 pm_runtime_set_active(max8998->dev);
164
165 switch (id->driver_data) {
166 case TYPE_LP3974:
167 ret = mfd_add_devices(max8998->dev, -1,
168 lp3974_devs, ARRAY_SIZE(lp3974_devs),
169 NULL, 0);
170 break;
171 case TYPE_MAX8998:
172 ret = mfd_add_devices(max8998->dev, -1,
173 max8998_devs, ARRAY_SIZE(max8998_devs),
174 NULL, 0);
175 break;
176 default:
177 ret = -EINVAL;
178 }
179
180 if (ret < 0)
181 goto err;
182
183 device_init_wakeup(max8998->dev, max8998->wakeup);
184
185 return ret;
186
187 err:
188 mfd_remove_devices(max8998->dev);
189 max8998_irq_exit(max8998);
190 i2c_unregister_device(max8998->rtc);
191 kfree(max8998);
192 return ret;
193 }
194
max8998_i2c_remove(struct i2c_client * i2c)195 static int max8998_i2c_remove(struct i2c_client *i2c)
196 {
197 struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
198
199 mfd_remove_devices(max8998->dev);
200 max8998_irq_exit(max8998);
201 i2c_unregister_device(max8998->rtc);
202 kfree(max8998);
203
204 return 0;
205 }
206
207 static const struct i2c_device_id max8998_i2c_id[] = {
208 { "max8998", TYPE_MAX8998 },
209 { "lp3974", TYPE_LP3974},
210 { }
211 };
212 MODULE_DEVICE_TABLE(i2c, max8998_i2c_id);
213
max8998_suspend(struct device * dev)214 static int max8998_suspend(struct device *dev)
215 {
216 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
217 struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
218
219 if (device_may_wakeup(dev))
220 irq_set_irq_wake(max8998->irq, 1);
221 return 0;
222 }
223
max8998_resume(struct device * dev)224 static int max8998_resume(struct device *dev)
225 {
226 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
227 struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
228
229 if (device_may_wakeup(dev))
230 irq_set_irq_wake(max8998->irq, 0);
231 /*
232 * In LP3974, if IRQ registers are not "read & clear"
233 * when it's set during sleep, the interrupt becomes
234 * disabled.
235 */
236 return max8998_irq_resume(i2c_get_clientdata(i2c));
237 }
238
239 struct max8998_reg_dump {
240 u8 addr;
241 u8 val;
242 };
243 #define SAVE_ITEM(x) { .addr = (x), .val = 0x0, }
244 static struct max8998_reg_dump max8998_dump[] = {
245 SAVE_ITEM(MAX8998_REG_IRQM1),
246 SAVE_ITEM(MAX8998_REG_IRQM2),
247 SAVE_ITEM(MAX8998_REG_IRQM3),
248 SAVE_ITEM(MAX8998_REG_IRQM4),
249 SAVE_ITEM(MAX8998_REG_STATUSM1),
250 SAVE_ITEM(MAX8998_REG_STATUSM2),
251 SAVE_ITEM(MAX8998_REG_CHGR1),
252 SAVE_ITEM(MAX8998_REG_CHGR2),
253 SAVE_ITEM(MAX8998_REG_LDO_ACTIVE_DISCHARGE1),
254 SAVE_ITEM(MAX8998_REG_LDO_ACTIVE_DISCHARGE1),
255 SAVE_ITEM(MAX8998_REG_BUCK_ACTIVE_DISCHARGE3),
256 SAVE_ITEM(MAX8998_REG_ONOFF1),
257 SAVE_ITEM(MAX8998_REG_ONOFF2),
258 SAVE_ITEM(MAX8998_REG_ONOFF3),
259 SAVE_ITEM(MAX8998_REG_ONOFF4),
260 SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE1),
261 SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE2),
262 SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE3),
263 SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE4),
264 SAVE_ITEM(MAX8998_REG_BUCK2_VOLTAGE1),
265 SAVE_ITEM(MAX8998_REG_BUCK2_VOLTAGE2),
266 SAVE_ITEM(MAX8998_REG_LDO2_LDO3),
267 SAVE_ITEM(MAX8998_REG_LDO4),
268 SAVE_ITEM(MAX8998_REG_LDO5),
269 SAVE_ITEM(MAX8998_REG_LDO6),
270 SAVE_ITEM(MAX8998_REG_LDO7),
271 SAVE_ITEM(MAX8998_REG_LDO8_LDO9),
272 SAVE_ITEM(MAX8998_REG_LDO10_LDO11),
273 SAVE_ITEM(MAX8998_REG_LDO12),
274 SAVE_ITEM(MAX8998_REG_LDO13),
275 SAVE_ITEM(MAX8998_REG_LDO14),
276 SAVE_ITEM(MAX8998_REG_LDO15),
277 SAVE_ITEM(MAX8998_REG_LDO16),
278 SAVE_ITEM(MAX8998_REG_LDO17),
279 SAVE_ITEM(MAX8998_REG_BKCHR),
280 SAVE_ITEM(MAX8998_REG_LBCNFG1),
281 SAVE_ITEM(MAX8998_REG_LBCNFG2),
282 };
283 /* Save registers before hibernation */
max8998_freeze(struct device * dev)284 static int max8998_freeze(struct device *dev)
285 {
286 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
287 int i;
288
289 for (i = 0; i < ARRAY_SIZE(max8998_dump); i++)
290 max8998_read_reg(i2c, max8998_dump[i].addr,
291 &max8998_dump[i].val);
292
293 return 0;
294 }
295
296 /* Restore registers after hibernation */
max8998_restore(struct device * dev)297 static int max8998_restore(struct device *dev)
298 {
299 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
300 int i;
301
302 for (i = 0; i < ARRAY_SIZE(max8998_dump); i++)
303 max8998_write_reg(i2c, max8998_dump[i].addr,
304 max8998_dump[i].val);
305
306 return 0;
307 }
308
309 static const struct dev_pm_ops max8998_pm = {
310 .suspend = max8998_suspend,
311 .resume = max8998_resume,
312 .freeze = max8998_freeze,
313 .restore = max8998_restore,
314 };
315
316 static struct i2c_driver max8998_i2c_driver = {
317 .driver = {
318 .name = "max8998",
319 .owner = THIS_MODULE,
320 .pm = &max8998_pm,
321 },
322 .probe = max8998_i2c_probe,
323 .remove = max8998_i2c_remove,
324 .id_table = max8998_i2c_id,
325 };
326
max8998_i2c_init(void)327 static int __init max8998_i2c_init(void)
328 {
329 return i2c_add_driver(&max8998_i2c_driver);
330 }
331 /* init early so consumer devices can complete system boot */
332 subsys_initcall(max8998_i2c_init);
333
max8998_i2c_exit(void)334 static void __exit max8998_i2c_exit(void)
335 {
336 i2c_del_driver(&max8998_i2c_driver);
337 }
338 module_exit(max8998_i2c_exit);
339
340 MODULE_DESCRIPTION("MAXIM 8998 multi-function core driver");
341 MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
342 MODULE_LICENSE("GPL");
343