blob: cb83a7ab53e73b9825c4eb527211ab49ccd33a7e [file] [log] [blame]
MyungJoo Ham527e7e92011-03-04 15:50:26 +09001/*
2 * max8997.c - mfd core driver for the Maxim 8966 and 8997
3 *
4 * Copyright (C) 2011 Samsung Electronics
5 * MyungJoo Ham <myungjoo.ham@smasung.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * This driver is based on max8998.c
22 */
23
24#include <linux/slab.h>
25#include <linux/i2c.h>
MyungJoo Ham01fdaab2011-08-19 14:39:40 +090026#include <linux/interrupt.h>
MyungJoo Ham527e7e92011-03-04 15:50:26 +090027#include <linux/pm_runtime.h>
Paul Gortmaker4e36dd32011-07-03 15:13:27 -040028#include <linux/module.h>
MyungJoo Ham527e7e92011-03-04 15:50:26 +090029#include <linux/mutex.h>
30#include <linux/mfd/core.h>
31#include <linux/mfd/max8997.h>
32#include <linux/mfd/max8997-private.h>
33
34#define I2C_ADDR_PMIC (0xCC >> 1)
35#define I2C_ADDR_MUIC (0x4A >> 1)
36#define I2C_ADDR_BATTERY (0x6C >> 1)
37#define I2C_ADDR_RTC (0x0C >> 1)
38#define I2C_ADDR_HAPTIC (0x90 >> 1)
39
40static struct mfd_cell max8997_devs[] = {
41 { .name = "max8997-pmic", },
42 { .name = "max8997-rtc", },
43 { .name = "max8997-battery", },
44 { .name = "max8997-haptic", },
45 { .name = "max8997-muic", },
Donggeun Kimf6dd2db2011-12-14 18:23:55 +090046 { .name = "max8997-led", .id = 1 },
47 { .name = "max8997-led", .id = 2 },
MyungJoo Ham527e7e92011-03-04 15:50:26 +090048};
49
50int max8997_read_reg(struct i2c_client *i2c, u8 reg, u8 *dest)
51{
52 struct max8997_dev *max8997 = i2c_get_clientdata(i2c);
53 int ret;
54
55 mutex_lock(&max8997->iolock);
56 ret = i2c_smbus_read_byte_data(i2c, reg);
57 mutex_unlock(&max8997->iolock);
58 if (ret < 0)
59 return ret;
60
61 ret &= 0xff;
62 *dest = ret;
63 return 0;
64}
65EXPORT_SYMBOL_GPL(max8997_read_reg);
66
67int max8997_bulk_read(struct i2c_client *i2c, u8 reg, int count, u8 *buf)
68{
69 struct max8997_dev *max8997 = i2c_get_clientdata(i2c);
70 int ret;
71
72 mutex_lock(&max8997->iolock);
73 ret = i2c_smbus_read_i2c_block_data(i2c, reg, count, buf);
74 mutex_unlock(&max8997->iolock);
75 if (ret < 0)
76 return ret;
77
78 return 0;
79}
80EXPORT_SYMBOL_GPL(max8997_bulk_read);
81
82int max8997_write_reg(struct i2c_client *i2c, u8 reg, u8 value)
83{
84 struct max8997_dev *max8997 = i2c_get_clientdata(i2c);
85 int ret;
86
87 mutex_lock(&max8997->iolock);
88 ret = i2c_smbus_write_byte_data(i2c, reg, value);
89 mutex_unlock(&max8997->iolock);
90 return ret;
91}
92EXPORT_SYMBOL_GPL(max8997_write_reg);
93
94int max8997_bulk_write(struct i2c_client *i2c, u8 reg, int count, u8 *buf)
95{
96 struct max8997_dev *max8997 = i2c_get_clientdata(i2c);
97 int ret;
98
99 mutex_lock(&max8997->iolock);
100 ret = i2c_smbus_write_i2c_block_data(i2c, reg, count, buf);
101 mutex_unlock(&max8997->iolock);
102 if (ret < 0)
103 return ret;
104
105 return 0;
106}
107EXPORT_SYMBOL_GPL(max8997_bulk_write);
108
109int max8997_update_reg(struct i2c_client *i2c, u8 reg, u8 val, u8 mask)
110{
111 struct max8997_dev *max8997 = i2c_get_clientdata(i2c);
112 int ret;
113
114 mutex_lock(&max8997->iolock);
115 ret = i2c_smbus_read_byte_data(i2c, reg);
116 if (ret >= 0) {
117 u8 old_val = ret & 0xff;
118 u8 new_val = (val & mask) | (old_val & (~mask));
119 ret = i2c_smbus_write_byte_data(i2c, reg, new_val);
120 }
121 mutex_unlock(&max8997->iolock);
122 return ret;
123}
124EXPORT_SYMBOL_GPL(max8997_update_reg);
125
126static int max8997_i2c_probe(struct i2c_client *i2c,
127 const struct i2c_device_id *id)
128{
129 struct max8997_dev *max8997;
130 struct max8997_platform_data *pdata = i2c->dev.platform_data;
131 int ret = 0;
132
133 max8997 = kzalloc(sizeof(struct max8997_dev), GFP_KERNEL);
134 if (max8997 == NULL)
135 return -ENOMEM;
136
137 i2c_set_clientdata(i2c, max8997);
138 max8997->dev = &i2c->dev;
139 max8997->i2c = i2c;
140 max8997->type = id->driver_data;
MyungJoo Ham7eb31542011-08-18 16:37:35 +0900141 max8997->irq = i2c->irq;
MyungJoo Ham527e7e92011-03-04 15:50:26 +0900142
143 if (!pdata)
144 goto err;
145
MyungJoo Ham7eb31542011-08-18 16:37:35 +0900146 max8997->irq_base = pdata->irq_base;
147 max8997->ono = pdata->ono;
MyungJoo Ham527e7e92011-03-04 15:50:26 +0900148
149 mutex_init(&max8997->iolock);
150
151 max8997->rtc = i2c_new_dummy(i2c->adapter, I2C_ADDR_RTC);
152 i2c_set_clientdata(max8997->rtc, max8997);
153 max8997->haptic = i2c_new_dummy(i2c->adapter, I2C_ADDR_HAPTIC);
154 i2c_set_clientdata(max8997->haptic, max8997);
155 max8997->muic = i2c_new_dummy(i2c->adapter, I2C_ADDR_MUIC);
156 i2c_set_clientdata(max8997->muic, max8997);
157
158 pm_runtime_set_active(max8997->dev);
159
MyungJoo Ham7eb31542011-08-18 16:37:35 +0900160 max8997_irq_init(max8997);
161
MyungJoo Ham527e7e92011-03-04 15:50:26 +0900162 mfd_add_devices(max8997->dev, -1, max8997_devs,
163 ARRAY_SIZE(max8997_devs),
164 NULL, 0);
165
166 /*
167 * TODO: enable others (flash, muic, rtc, battery, ...) and
168 * check the return value
169 */
170
171 if (ret < 0)
172 goto err_mfd;
173
MyungJoo Ham01fdaab2011-08-19 14:39:40 +0900174 /* MAX8997 has a power button input. */
175 device_init_wakeup(max8997->dev, pdata->wakeup);
176
MyungJoo Ham527e7e92011-03-04 15:50:26 +0900177 return ret;
178
179err_mfd:
180 mfd_remove_devices(max8997->dev);
181 i2c_unregister_device(max8997->muic);
182 i2c_unregister_device(max8997->haptic);
183 i2c_unregister_device(max8997->rtc);
184err:
185 kfree(max8997);
186 return ret;
187}
188
189static int max8997_i2c_remove(struct i2c_client *i2c)
190{
191 struct max8997_dev *max8997 = i2c_get_clientdata(i2c);
192
193 mfd_remove_devices(max8997->dev);
194 i2c_unregister_device(max8997->muic);
195 i2c_unregister_device(max8997->haptic);
196 i2c_unregister_device(max8997->rtc);
197 kfree(max8997);
198
199 return 0;
200}
201
202static const struct i2c_device_id max8997_i2c_id[] = {
203 { "max8997", TYPE_MAX8997 },
204 { "max8966", TYPE_MAX8966 },
205 { }
206};
207MODULE_DEVICE_TABLE(i2c, max8998_i2c_id);
208
209u8 max8997_dumpaddr_pmic[] = {
210 MAX8997_REG_INT1MSK,
211 MAX8997_REG_INT2MSK,
212 MAX8997_REG_INT3MSK,
213 MAX8997_REG_INT4MSK,
214 MAX8997_REG_MAINCON1,
215 MAX8997_REG_MAINCON2,
216 MAX8997_REG_BUCKRAMP,
217 MAX8997_REG_BUCK1CTRL,
218 MAX8997_REG_BUCK1DVS1,
219 MAX8997_REG_BUCK1DVS2,
220 MAX8997_REG_BUCK1DVS3,
221 MAX8997_REG_BUCK1DVS4,
222 MAX8997_REG_BUCK1DVS5,
223 MAX8997_REG_BUCK1DVS6,
224 MAX8997_REG_BUCK1DVS7,
225 MAX8997_REG_BUCK1DVS8,
226 MAX8997_REG_BUCK2CTRL,
227 MAX8997_REG_BUCK2DVS1,
228 MAX8997_REG_BUCK2DVS2,
229 MAX8997_REG_BUCK2DVS3,
230 MAX8997_REG_BUCK2DVS4,
231 MAX8997_REG_BUCK2DVS5,
232 MAX8997_REG_BUCK2DVS6,
233 MAX8997_REG_BUCK2DVS7,
234 MAX8997_REG_BUCK2DVS8,
235 MAX8997_REG_BUCK3CTRL,
236 MAX8997_REG_BUCK3DVS,
237 MAX8997_REG_BUCK4CTRL,
238 MAX8997_REG_BUCK4DVS,
239 MAX8997_REG_BUCK5CTRL,
240 MAX8997_REG_BUCK5DVS1,
241 MAX8997_REG_BUCK5DVS2,
242 MAX8997_REG_BUCK5DVS3,
243 MAX8997_REG_BUCK5DVS4,
244 MAX8997_REG_BUCK5DVS5,
245 MAX8997_REG_BUCK5DVS6,
246 MAX8997_REG_BUCK5DVS7,
247 MAX8997_REG_BUCK5DVS8,
248 MAX8997_REG_BUCK6CTRL,
249 MAX8997_REG_BUCK6BPSKIPCTRL,
250 MAX8997_REG_BUCK7CTRL,
251 MAX8997_REG_BUCK7DVS,
252 MAX8997_REG_LDO1CTRL,
253 MAX8997_REG_LDO2CTRL,
254 MAX8997_REG_LDO3CTRL,
255 MAX8997_REG_LDO4CTRL,
256 MAX8997_REG_LDO5CTRL,
257 MAX8997_REG_LDO6CTRL,
258 MAX8997_REG_LDO7CTRL,
259 MAX8997_REG_LDO8CTRL,
260 MAX8997_REG_LDO9CTRL,
261 MAX8997_REG_LDO10CTRL,
262 MAX8997_REG_LDO11CTRL,
263 MAX8997_REG_LDO12CTRL,
264 MAX8997_REG_LDO13CTRL,
265 MAX8997_REG_LDO14CTRL,
266 MAX8997_REG_LDO15CTRL,
267 MAX8997_REG_LDO16CTRL,
268 MAX8997_REG_LDO17CTRL,
269 MAX8997_REG_LDO18CTRL,
270 MAX8997_REG_LDO21CTRL,
271 MAX8997_REG_MBCCTRL1,
272 MAX8997_REG_MBCCTRL2,
273 MAX8997_REG_MBCCTRL3,
274 MAX8997_REG_MBCCTRL4,
275 MAX8997_REG_MBCCTRL5,
276 MAX8997_REG_MBCCTRL6,
277 MAX8997_REG_OTPCGHCVS,
278 MAX8997_REG_SAFEOUTCTRL,
279 MAX8997_REG_LBCNFG1,
280 MAX8997_REG_LBCNFG2,
281 MAX8997_REG_BBCCTRL,
282
283 MAX8997_REG_FLASH1_CUR,
284 MAX8997_REG_FLASH2_CUR,
285 MAX8997_REG_MOVIE_CUR,
286 MAX8997_REG_GSMB_CUR,
287 MAX8997_REG_BOOST_CNTL,
288 MAX8997_REG_LEN_CNTL,
289 MAX8997_REG_FLASH_CNTL,
290 MAX8997_REG_WDT_CNTL,
291 MAX8997_REG_MAXFLASH1,
292 MAX8997_REG_MAXFLASH2,
293 MAX8997_REG_FLASHSTATUSMASK,
294
295 MAX8997_REG_GPIOCNTL1,
296 MAX8997_REG_GPIOCNTL2,
297 MAX8997_REG_GPIOCNTL3,
298 MAX8997_REG_GPIOCNTL4,
299 MAX8997_REG_GPIOCNTL5,
300 MAX8997_REG_GPIOCNTL6,
301 MAX8997_REG_GPIOCNTL7,
302 MAX8997_REG_GPIOCNTL8,
303 MAX8997_REG_GPIOCNTL9,
304 MAX8997_REG_GPIOCNTL10,
305 MAX8997_REG_GPIOCNTL11,
306 MAX8997_REG_GPIOCNTL12,
307
308 MAX8997_REG_LDO1CONFIG,
309 MAX8997_REG_LDO2CONFIG,
310 MAX8997_REG_LDO3CONFIG,
311 MAX8997_REG_LDO4CONFIG,
312 MAX8997_REG_LDO5CONFIG,
313 MAX8997_REG_LDO6CONFIG,
314 MAX8997_REG_LDO7CONFIG,
315 MAX8997_REG_LDO8CONFIG,
316 MAX8997_REG_LDO9CONFIG,
317 MAX8997_REG_LDO10CONFIG,
318 MAX8997_REG_LDO11CONFIG,
319 MAX8997_REG_LDO12CONFIG,
320 MAX8997_REG_LDO13CONFIG,
321 MAX8997_REG_LDO14CONFIG,
322 MAX8997_REG_LDO15CONFIG,
323 MAX8997_REG_LDO16CONFIG,
324 MAX8997_REG_LDO17CONFIG,
325 MAX8997_REG_LDO18CONFIG,
326 MAX8997_REG_LDO21CONFIG,
327
328 MAX8997_REG_DVSOKTIMER1,
329 MAX8997_REG_DVSOKTIMER2,
330 MAX8997_REG_DVSOKTIMER4,
331 MAX8997_REG_DVSOKTIMER5,
332};
333
334u8 max8997_dumpaddr_muic[] = {
335 MAX8997_MUIC_REG_INTMASK1,
336 MAX8997_MUIC_REG_INTMASK2,
337 MAX8997_MUIC_REG_INTMASK3,
338 MAX8997_MUIC_REG_CDETCTRL,
339 MAX8997_MUIC_REG_CONTROL1,
340 MAX8997_MUIC_REG_CONTROL2,
341 MAX8997_MUIC_REG_CONTROL3,
342};
343
344u8 max8997_dumpaddr_haptic[] = {
345 MAX8997_HAPTIC_REG_CONF1,
346 MAX8997_HAPTIC_REG_CONF2,
347 MAX8997_HAPTIC_REG_DRVCONF,
348 MAX8997_HAPTIC_REG_CYCLECONF1,
349 MAX8997_HAPTIC_REG_CYCLECONF2,
350 MAX8997_HAPTIC_REG_SIGCONF1,
351 MAX8997_HAPTIC_REG_SIGCONF2,
352 MAX8997_HAPTIC_REG_SIGCONF3,
353 MAX8997_HAPTIC_REG_SIGCONF4,
354 MAX8997_HAPTIC_REG_SIGDC1,
355 MAX8997_HAPTIC_REG_SIGDC2,
356 MAX8997_HAPTIC_REG_SIGPWMDC1,
357 MAX8997_HAPTIC_REG_SIGPWMDC2,
358 MAX8997_HAPTIC_REG_SIGPWMDC3,
359 MAX8997_HAPTIC_REG_SIGPWMDC4,
360};
361
362static int max8997_freeze(struct device *dev)
363{
364 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
365 struct max8997_dev *max8997 = i2c_get_clientdata(i2c);
366 int i;
367
368 for (i = 0; i < ARRAY_SIZE(max8997_dumpaddr_pmic); i++)
369 max8997_read_reg(i2c, max8997_dumpaddr_pmic[i],
370 &max8997->reg_dump[i]);
371
372 for (i = 0; i < ARRAY_SIZE(max8997_dumpaddr_muic); i++)
373 max8997_read_reg(i2c, max8997_dumpaddr_muic[i],
374 &max8997->reg_dump[i + MAX8997_REG_PMIC_END]);
375
376 for (i = 0; i < ARRAY_SIZE(max8997_dumpaddr_haptic); i++)
377 max8997_read_reg(i2c, max8997_dumpaddr_haptic[i],
378 &max8997->reg_dump[i + MAX8997_REG_PMIC_END +
379 MAX8997_MUIC_REG_END]);
380
381 return 0;
382}
383
384static int max8997_restore(struct device *dev)
385{
386 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
387 struct max8997_dev *max8997 = i2c_get_clientdata(i2c);
388 int i;
389
390 for (i = 0; i < ARRAY_SIZE(max8997_dumpaddr_pmic); i++)
391 max8997_write_reg(i2c, max8997_dumpaddr_pmic[i],
392 max8997->reg_dump[i]);
393
394 for (i = 0; i < ARRAY_SIZE(max8997_dumpaddr_muic); i++)
395 max8997_write_reg(i2c, max8997_dumpaddr_muic[i],
396 max8997->reg_dump[i + MAX8997_REG_PMIC_END]);
397
398 for (i = 0; i < ARRAY_SIZE(max8997_dumpaddr_haptic); i++)
399 max8997_write_reg(i2c, max8997_dumpaddr_haptic[i],
400 max8997->reg_dump[i + MAX8997_REG_PMIC_END +
401 MAX8997_MUIC_REG_END]);
402
403 return 0;
404}
405
MyungJoo Ham01fdaab2011-08-19 14:39:40 +0900406static int max8997_suspend(struct device *dev)
407{
408 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
409 struct max8997_dev *max8997 = i2c_get_clientdata(i2c);
410
411 if (device_may_wakeup(dev))
412 irq_set_irq_wake(max8997->irq, 1);
413 return 0;
414}
415
416static int max8997_resume(struct device *dev)
417{
418 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
419 struct max8997_dev *max8997 = i2c_get_clientdata(i2c);
420
421 if (device_may_wakeup(dev))
422 irq_set_irq_wake(max8997->irq, 0);
423 return max8997_irq_resume(max8997);
424}
425
MyungJoo Ham527e7e92011-03-04 15:50:26 +0900426const struct dev_pm_ops max8997_pm = {
MyungJoo Ham01fdaab2011-08-19 14:39:40 +0900427 .suspend = max8997_suspend,
428 .resume = max8997_resume,
MyungJoo Ham527e7e92011-03-04 15:50:26 +0900429 .freeze = max8997_freeze,
430 .restore = max8997_restore,
431};
432
433static struct i2c_driver max8997_i2c_driver = {
434 .driver = {
435 .name = "max8997",
436 .owner = THIS_MODULE,
437 .pm = &max8997_pm,
438 },
439 .probe = max8997_i2c_probe,
440 .remove = max8997_i2c_remove,
441 .id_table = max8997_i2c_id,
442};
443
444static int __init max8997_i2c_init(void)
445{
446 return i2c_add_driver(&max8997_i2c_driver);
447}
448/* init early so consumer devices can complete system boot */
449subsys_initcall(max8997_i2c_init);
450
451static void __exit max8997_i2c_exit(void)
452{
453 i2c_del_driver(&max8997_i2c_driver);
454}
455module_exit(max8997_i2c_exit);
456
457MODULE_DESCRIPTION("MAXIM 8997 multi-function core driver");
458MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
459MODULE_LICENSE("GPL");