2 * max77686.c - mfd core driver for the Maxim 77686
4 * Copyright (C) 2012 Samsung Electronics
5 * Chiwoong Byun <woong.byun@samsung.com>
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.
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.
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
21 * This driver is based on max8997.c
24 #include <linux/module.h>
25 #include <linux/slab.h>
26 #include <linux/i2c.h>
27 #include <linux/interrupt.h>
28 #include <linux/err.h>
29 #include <linux/pm_runtime.h>
30 #include <linux/mutex.h>
31 #include <linux/mfd/core.h>
32 #include <linux/mfd/max77686.h>
33 #include <linux/mfd/max77686-private.h>
35 #define I2C_ADDR_RTC (0x0C >> 1)
38 static struct of_device_id __devinitdata max77686_pmic_dt_match[] = {
39 {.compatible = "maxim,max77686-pmic", .data = TYPE_MAX77686},
44 static struct mfd_cell max77686_devs[] = {
45 {.name = "max77686-pmic",},
46 {.name = "max77686-rtc",},
49 int max77686_read_reg(struct i2c_client *i2c, u8 reg, u8 *dest)
51 struct max77686_dev *max77686 = i2c_get_clientdata(i2c);
54 mutex_lock(&max77686->iolock);
55 ret = i2c_smbus_read_byte_data(i2c, reg);
56 mutex_unlock(&max77686->iolock);
64 EXPORT_SYMBOL_GPL(max77686_read_reg);
66 int max77686_bulk_read(struct i2c_client *i2c, u8 reg, int count, u8 *buf)
68 struct max77686_dev *max77686 = i2c_get_clientdata(i2c);
71 mutex_lock(&max77686->iolock);
72 ret = i2c_smbus_read_i2c_block_data(i2c, reg, count, buf);
73 mutex_unlock(&max77686->iolock);
79 EXPORT_SYMBOL_GPL(max77686_bulk_read);
81 int max77686_write_reg(struct i2c_client *i2c, u8 reg, u8 value)
83 struct max77686_dev *max77686 = i2c_get_clientdata(i2c);
86 mutex_lock(&max77686->iolock);
87 ret = i2c_smbus_write_byte_data(i2c, reg, value);
88 mutex_unlock(&max77686->iolock);
91 EXPORT_SYMBOL_GPL(max77686_write_reg);
93 int max77686_bulk_write(struct i2c_client *i2c, u8 reg, int count, u8 *buf)
95 struct max77686_dev *max77686 = i2c_get_clientdata(i2c);
98 mutex_lock(&max77686->iolock);
99 ret = i2c_smbus_write_i2c_block_data(i2c, reg, count, buf);
100 mutex_unlock(&max77686->iolock);
106 EXPORT_SYMBOL_GPL(max77686_bulk_write);
108 int max77686_update_reg(struct i2c_client *i2c, u8 reg, u8 val, u8 mask)
110 struct max77686_dev *max77686 = i2c_get_clientdata(i2c);
114 return max77686_write_reg(i2c, reg, val);
116 mutex_lock(&max77686->iolock);
117 ret = i2c_smbus_read_byte_data(i2c, reg);
119 u8 old_val = ret & 0xff;
120 u8 new_val = (val & mask) | (old_val & (~mask));
121 ret = i2c_smbus_write_byte_data(i2c, reg, new_val);
123 mutex_unlock(&max77686->iolock);
126 EXPORT_SYMBOL_GPL(max77686_update_reg);
129 static struct max77686_platform_data *max77686_i2c_parse_dt_pdata(struct device
132 struct max77686_platform_data *pd;
134 pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
136 dev_err(dev, "could not allocate memory for pdata\n");
137 return ERR_PTR(-ENOMEM);
143 static struct max77686_platform_data *max77686_i2c_parse_dt_pdata(struct device
150 static inline int max77686_i2c_get_driver_data(struct i2c_client *i2c,
151 const struct i2c_device_id *id)
154 if (i2c->dev.of_node) {
155 const struct of_device_id *match;
156 match = of_match_node(max77686_pmic_dt_match,
158 return (int)match->data;
161 return (int)id->driver_data;
164 static int max77686_i2c_probe(struct i2c_client *i2c,
165 const struct i2c_device_id *id)
167 struct max77686_dev *max77686;
168 struct max77686_platform_data *pdata = i2c->dev.platform_data;
172 max77686 = kzalloc(sizeof(struct max77686_dev), GFP_KERNEL);
173 if (max77686 == NULL) {
174 dev_err(max77686->dev, "could not allocate memory\n");
178 max77686->dev = &i2c->dev;
180 if (max77686->dev->of_node) {
181 pdata = max77686_i2c_parse_dt_pdata(max77686->dev);
183 ret = PTR_ERR(pdata);
190 dbg_info("%s : No platform data found\n", __func__);
194 i2c_set_clientdata(i2c, max77686);
196 max77686->irq = i2c->irq;
197 max77686->type = max77686_i2c_get_driver_data(i2c, id);
199 max77686->pdata = pdata;
201 mutex_init(&max77686->iolock);
203 max77686->rtc = i2c_new_dummy(i2c->adapter, I2C_ADDR_RTC);
204 i2c_set_clientdata(max77686->rtc, max77686);
205 max77686_irq_init(max77686);
207 ret = mfd_add_devices(max77686->dev, -1, max77686_devs,
208 ARRAY_SIZE(max77686_devs), NULL, 0);
211 dbg_info("%s : mfd_add_devices failed\n", __func__);
215 pm_runtime_set_active(max77686->dev);
217 if (max77686_read_reg(i2c, MAX77686_REG_DEVICE_ID, &data) < 0) {
219 dbg_info("%s : device not found on this channel\n", __func__);
222 dev_info(max77686->dev, "device found\n");
227 mfd_remove_devices(max77686->dev);
228 max77686_irq_exit(max77686);
229 i2c_unregister_device(max77686->rtc);
232 dev_err(max77686->dev, "device probe failed : %d\n", ret);
236 static int max77686_i2c_remove(struct i2c_client *i2c)
238 struct max77686_dev *max77686 = i2c_get_clientdata(i2c);
240 device_init_wakeup(max77686->dev, 0);
241 pm_runtime_set_suspended(max77686->dev);
242 mfd_remove_devices(max77686->dev);
243 max77686_irq_exit(max77686);
244 i2c_unregister_device(max77686->rtc);
249 static const struct i2c_device_id max77686_i2c_id[] = {
250 {"max77686", TYPE_MAX77686},
254 MODULE_DEVICE_TABLE(i2c, max77686_i2c_id);
256 static int max77686_suspend(struct device *dev)
258 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
259 struct max77686_dev *max77686 = i2c_get_clientdata(i2c);
261 if (device_may_wakeup(dev))
262 enable_irq_wake(max77686->irq);
267 static int max77686_resume(struct device *dev)
269 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
270 struct max77686_dev *max77686 = i2c_get_clientdata(i2c);
272 if (device_may_wakeup(dev))
273 disable_irq_wake(max77686->irq);
275 max77686_irq_resume(max77686);
279 const struct dev_pm_ops max77686_pm = {
280 .suspend = max77686_suspend,
281 .resume = max77686_resume,
284 static struct i2c_driver max77686_i2c_driver = {
287 .owner = THIS_MODULE,
289 .of_match_table = of_match_ptr(max77686_pmic_dt_match),
291 .probe = max77686_i2c_probe,
292 .remove = max77686_i2c_remove,
293 .id_table = max77686_i2c_id,
296 static int __init max77686_i2c_init(void)
298 return i2c_add_driver(&max77686_i2c_driver);
301 subsys_initcall(max77686_i2c_init);
303 static void __exit max77686_i2c_exit(void)
305 i2c_del_driver(&max77686_i2c_driver);
308 module_exit(max77686_i2c_exit);
310 MODULE_DESCRIPTION("MAXIM 77686 multi-function core driver");
311 MODULE_AUTHOR("Chiwoong Byun <woong.byun@samsung.com>");
312 MODULE_LICENSE("GPL");