blob: 49c140a78ba9b260491185c88458dea4bb1b5c61 [file] [log] [blame]
Kyungmin Park156f2522010-06-16 09:04:16 +02001/*
Joonyoung Shim2c7e6f52010-09-10 18:36:39 +02002 * max8998.c - mfd core driver for the Maxim 8998
Kyungmin Park156f2522010-06-16 09:04:16 +02003 *
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/mutex.h>
29#include <linux/mfd/core.h>
30#include <linux/mfd/max8998.h>
31#include <linux/mfd/max8998-private.h>
32
33static struct mfd_cell max8998_devs[] = {
34 {
35 .name = "max8998-pmic",
36 }
37};
38
Joonyoung Shim676e02d2010-08-06 11:28:06 +090039int max8998_read_reg(struct i2c_client *i2c, u8 reg, u8 *dest)
Kyungmin Park156f2522010-06-16 09:04:16 +020040{
Joonyoung Shim676e02d2010-08-06 11:28:06 +090041 struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
Kyungmin Park156f2522010-06-16 09:04:16 +020042 int ret;
43
44 mutex_lock(&max8998->iolock);
Joonyoung Shim676e02d2010-08-06 11:28:06 +090045 ret = i2c_smbus_read_byte_data(i2c, reg);
Kyungmin Park156f2522010-06-16 09:04:16 +020046 mutex_unlock(&max8998->iolock);
47 if (ret < 0)
48 return ret;
49
50 ret &= 0xff;
51 *dest = ret;
52 return 0;
53}
Joonyoung Shim676e02d2010-08-06 11:28:06 +090054EXPORT_SYMBOL(max8998_read_reg);
Kyungmin Park156f2522010-06-16 09:04:16 +020055
Joonyoung Shim2c7e6f52010-09-10 18:36:39 +020056int max8998_bulk_read(struct i2c_client *i2c, u8 reg, int count, u8 *buf)
57{
58 struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
59 int ret;
60
61 mutex_lock(&max8998->iolock);
62 ret = i2c_smbus_read_i2c_block_data(i2c, reg, count, buf);
63 mutex_unlock(&max8998->iolock);
64 if (ret < 0)
65 return ret;
66
67 return 0;
68}
69EXPORT_SYMBOL(max8998_bulk_read);
70
Joonyoung Shim676e02d2010-08-06 11:28:06 +090071int max8998_write_reg(struct i2c_client *i2c, u8 reg, u8 value)
Kyungmin Park156f2522010-06-16 09:04:16 +020072{
Joonyoung Shim676e02d2010-08-06 11:28:06 +090073 struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
Kyungmin Park156f2522010-06-16 09:04:16 +020074 int ret;
75
76 mutex_lock(&max8998->iolock);
Joonyoung Shim676e02d2010-08-06 11:28:06 +090077 ret = i2c_smbus_write_byte_data(i2c, reg, value);
Kyungmin Park156f2522010-06-16 09:04:16 +020078 mutex_unlock(&max8998->iolock);
79 return ret;
80}
Joonyoung Shim676e02d2010-08-06 11:28:06 +090081EXPORT_SYMBOL(max8998_write_reg);
Kyungmin Park156f2522010-06-16 09:04:16 +020082
Joonyoung Shim676e02d2010-08-06 11:28:06 +090083int max8998_update_reg(struct i2c_client *i2c, u8 reg, u8 val, u8 mask)
Kyungmin Park156f2522010-06-16 09:04:16 +020084{
Joonyoung Shim676e02d2010-08-06 11:28:06 +090085 struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
Kyungmin Park156f2522010-06-16 09:04:16 +020086 int ret;
87
88 mutex_lock(&max8998->iolock);
Joonyoung Shim676e02d2010-08-06 11:28:06 +090089 ret = i2c_smbus_read_byte_data(i2c, reg);
Kyungmin Park156f2522010-06-16 09:04:16 +020090 if (ret >= 0) {
91 u8 old_val = ret & 0xff;
92 u8 new_val = (val & mask) | (old_val & (~mask));
Joonyoung Shim676e02d2010-08-06 11:28:06 +090093 ret = i2c_smbus_write_byte_data(i2c, reg, new_val);
Kyungmin Park156f2522010-06-16 09:04:16 +020094 if (ret >= 0)
95 ret = 0;
96 }
97 mutex_unlock(&max8998->iolock);
98 return ret;
99}
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900100EXPORT_SYMBOL(max8998_update_reg);
Kyungmin Park156f2522010-06-16 09:04:16 +0200101
102static int max8998_i2c_probe(struct i2c_client *i2c,
103 const struct i2c_device_id *id)
104{
Joonyoung Shim2c7e6f52010-09-10 18:36:39 +0200105 struct max8998_platform_data *pdata = i2c->dev.platform_data;
Kyungmin Park156f2522010-06-16 09:04:16 +0200106 struct max8998_dev *max8998;
107 int ret = 0;
108
109 max8998 = kzalloc(sizeof(struct max8998_dev), GFP_KERNEL);
Axel Lin8f1f1512010-08-09 14:48:16 +0800110 if (max8998 == NULL)
Kyungmin Park156f2522010-06-16 09:04:16 +0200111 return -ENOMEM;
Kyungmin Park156f2522010-06-16 09:04:16 +0200112
113 i2c_set_clientdata(i2c, max8998);
114 max8998->dev = &i2c->dev;
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900115 max8998->i2c = i2c;
Joonyoung Shim2c7e6f52010-09-10 18:36:39 +0200116 max8998->irq = i2c->irq;
117 if (pdata) {
118 max8998->ono = pdata->ono;
119 max8998->irq_base = pdata->irq_base;
120 }
Kyungmin Park156f2522010-06-16 09:04:16 +0200121 mutex_init(&max8998->iolock);
122
Joonyoung Shim2c7e6f52010-09-10 18:36:39 +0200123 max8998_irq_init(max8998);
124
Kyungmin Park156f2522010-06-16 09:04:16 +0200125 ret = mfd_add_devices(max8998->dev, -1,
126 max8998_devs, ARRAY_SIZE(max8998_devs),
127 NULL, 0);
128 if (ret < 0)
129 goto err;
130
131 return ret;
132
133err:
134 mfd_remove_devices(max8998->dev);
135 kfree(max8998);
136 return ret;
137}
138
139static int max8998_i2c_remove(struct i2c_client *i2c)
140{
141 struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
142
143 mfd_remove_devices(max8998->dev);
144 kfree(max8998);
145
146 return 0;
147}
148
149static const struct i2c_device_id max8998_i2c_id[] = {
Kyungmin Parkf8539dd2010-08-23 13:46:49 +0900150 { "max8998", 0 },
151 { "lp3974", 0 },
152 { }
Kyungmin Park156f2522010-06-16 09:04:16 +0200153};
154MODULE_DEVICE_TABLE(i2c, max8998_i2c_id);
155
156static struct i2c_driver max8998_i2c_driver = {
157 .driver = {
158 .name = "max8998",
159 .owner = THIS_MODULE,
160 },
161 .probe = max8998_i2c_probe,
162 .remove = max8998_i2c_remove,
163 .id_table = max8998_i2c_id,
164};
165
166static int __init max8998_i2c_init(void)
167{
168 return i2c_add_driver(&max8998_i2c_driver);
169}
170/* init early so consumer devices can complete system boot */
171subsys_initcall(max8998_i2c_init);
172
173static void __exit max8998_i2c_exit(void)
174{
175 i2c_del_driver(&max8998_i2c_driver);
176}
177module_exit(max8998_i2c_exit);
178
179MODULE_DESCRIPTION("MAXIM 8998 multi-function core driver");
180MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
181MODULE_LICENSE("GPL");