CHROMIUM: mfd: max77686: don't read when updating if mask is 0xff
[cascardo/linux.git] / drivers / mfd / max77686.c
1 /*
2  * max77686.c - mfd core driver for the Maxim 77686
3  *
4  * Copyright (C) 2012 Samsung Electronics
5  * Chiwoong Byun <woong.byun@samsung.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 max8997.c
22  */
23
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>
34
35 #define I2C_ADDR_RTC    (0x0C >> 1)
36
37 #ifdef CONFIG_OF
38 static struct of_device_id __devinitdata max77686_pmic_dt_match[] = {
39         {.compatible = "maxim,max77686-pmic",   .data = TYPE_MAX77686},
40         {},
41 };
42 #endif
43
44 static struct mfd_cell max77686_devs[] = {
45         {.name = "max77686-pmic",},
46         {.name = "max77686-rtc",},
47 };
48
49 int max77686_read_reg(struct i2c_client *i2c, u8 reg, u8 *dest)
50 {
51         struct max77686_dev *max77686 = i2c_get_clientdata(i2c);
52         int ret;
53
54         mutex_lock(&max77686->iolock);
55         ret = i2c_smbus_read_byte_data(i2c, reg);
56         mutex_unlock(&max77686->iolock);
57         if (ret < 0)
58                 return ret;
59
60         ret &= 0xff;
61         *dest = ret;
62         return 0;
63 }
64 EXPORT_SYMBOL_GPL(max77686_read_reg);
65
66 int max77686_bulk_read(struct i2c_client *i2c, u8 reg, int count, u8 *buf)
67 {
68         struct max77686_dev *max77686 = i2c_get_clientdata(i2c);
69         int ret;
70
71         mutex_lock(&max77686->iolock);
72         ret = i2c_smbus_read_i2c_block_data(i2c, reg, count, buf);
73         mutex_unlock(&max77686->iolock);
74         if (ret < 0)
75                 return ret;
76
77         return 0;
78 }
79 EXPORT_SYMBOL_GPL(max77686_bulk_read);
80
81 int max77686_write_reg(struct i2c_client *i2c, u8 reg, u8 value)
82 {
83         struct max77686_dev *max77686 = i2c_get_clientdata(i2c);
84         int ret;
85
86         mutex_lock(&max77686->iolock);
87         ret = i2c_smbus_write_byte_data(i2c, reg, value);
88         mutex_unlock(&max77686->iolock);
89         return ret;
90 }
91 EXPORT_SYMBOL_GPL(max77686_write_reg);
92
93 int max77686_bulk_write(struct i2c_client *i2c, u8 reg, int count, u8 *buf)
94 {
95         struct max77686_dev *max77686 = i2c_get_clientdata(i2c);
96         int ret;
97
98         mutex_lock(&max77686->iolock);
99         ret = i2c_smbus_write_i2c_block_data(i2c, reg, count, buf);
100         mutex_unlock(&max77686->iolock);
101         if (ret < 0)
102                 return ret;
103
104         return 0;
105 }
106 EXPORT_SYMBOL_GPL(max77686_bulk_write);
107
108 int max77686_update_reg(struct i2c_client *i2c, u8 reg, u8 val, u8 mask)
109 {
110         struct max77686_dev *max77686 = i2c_get_clientdata(i2c);
111         int ret;
112
113         if (mask == 0xff)
114                 return max77686_write_reg(i2c, reg, val);
115
116         mutex_lock(&max77686->iolock);
117         ret = i2c_smbus_read_byte_data(i2c, reg);
118         if (ret >= 0) {
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);
122         }
123         mutex_unlock(&max77686->iolock);
124         return ret;
125 }
126 EXPORT_SYMBOL_GPL(max77686_update_reg);
127
128 #ifdef CONFIG_OF
129 static struct max77686_platform_data *max77686_i2c_parse_dt_pdata(struct device
130                                                                   *dev)
131 {
132         struct max77686_platform_data *pd;
133
134         pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
135         if (!pd) {
136                 dev_err(dev, "could not allocate memory for pdata\n");
137                 return ERR_PTR(-ENOMEM);
138         }
139
140         return pd;
141 }
142 #else
143 static struct max77686_platform_data *max77686_i2c_parse_dt_pdata(struct device
144                                                                   *dev)
145 {
146         return 0;
147 }
148 #endif
149
150 static inline int max77686_i2c_get_driver_data(struct i2c_client *i2c,
151                                                const struct i2c_device_id *id)
152 {
153 #ifdef CONFIG_OF
154         if (i2c->dev.of_node) {
155                 const struct of_device_id *match;
156                 match = of_match_node(max77686_pmic_dt_match,
157                                 i2c->dev.of_node);
158                 return (int)match->data;
159         }
160 #endif
161         return (int)id->driver_data;
162 }
163
164 static int max77686_i2c_probe(struct i2c_client *i2c,
165                               const struct i2c_device_id *id)
166 {
167         struct max77686_dev *max77686;
168         struct max77686_platform_data *pdata = i2c->dev.platform_data;
169         int ret = 0;
170         u8 data;
171
172         max77686 = kzalloc(sizeof(struct max77686_dev), GFP_KERNEL);
173         if (max77686 == NULL) {
174                 dev_err(max77686->dev, "could not allocate memory\n");
175                 return -ENOMEM;
176         }
177
178         max77686->dev = &i2c->dev;
179
180         if (max77686->dev->of_node) {
181                 pdata = max77686_i2c_parse_dt_pdata(max77686->dev);
182                 if (IS_ERR(pdata)) {
183                         ret = PTR_ERR(pdata);
184                         goto err;
185                 }
186         }
187
188         if (!pdata) {
189                 ret = -ENODEV;
190                 dbg_info("%s : No platform data found\n", __func__);
191                 goto err;
192         }
193
194         i2c_set_clientdata(i2c, max77686);
195         max77686->i2c = i2c;
196         max77686->irq = i2c->irq;
197         max77686->type = max77686_i2c_get_driver_data(i2c, id);
198
199         max77686->pdata = pdata;
200
201         mutex_init(&max77686->iolock);
202
203         max77686->rtc = i2c_new_dummy(i2c->adapter, I2C_ADDR_RTC);
204         i2c_set_clientdata(max77686->rtc, max77686);
205         max77686_irq_init(max77686);
206
207         ret = mfd_add_devices(max77686->dev, -1, max77686_devs,
208                               ARRAY_SIZE(max77686_devs), NULL, 0);
209
210         if (ret < 0) {
211                 dbg_info("%s : mfd_add_devices failed\n", __func__);
212                 goto err_mfd;
213         }
214
215         pm_runtime_set_active(max77686->dev);
216
217         if (max77686_read_reg(i2c, MAX77686_REG_DEVICE_ID, &data) < 0) {
218                 ret = -EIO;
219                 dbg_info("%s : device not found on this channel\n", __func__);
220                 goto err_mfd;
221         } else
222                 dev_info(max77686->dev, "device found\n");
223
224         return ret;
225
226  err_mfd:
227         mfd_remove_devices(max77686->dev);
228         max77686_irq_exit(max77686);
229         i2c_unregister_device(max77686->rtc);
230  err:
231         kfree(max77686);
232         dev_err(max77686->dev, "device probe failed : %d\n", ret);
233         return ret;
234 }
235
236 static int max77686_i2c_remove(struct i2c_client *i2c)
237 {
238         struct max77686_dev *max77686 = i2c_get_clientdata(i2c);
239
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);
245         kfree(max77686);
246         return 0;
247 }
248
249 static const struct i2c_device_id max77686_i2c_id[] = {
250         {"max77686", TYPE_MAX77686},
251         {}
252 };
253
254 MODULE_DEVICE_TABLE(i2c, max77686_i2c_id);
255
256 static int max77686_suspend(struct device *dev)
257 {
258         struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
259         struct max77686_dev *max77686 = i2c_get_clientdata(i2c);
260
261         if (device_may_wakeup(dev))
262                 enable_irq_wake(max77686->irq);
263
264         return 0;
265 }
266
267 static int max77686_resume(struct device *dev)
268 {
269         struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
270         struct max77686_dev *max77686 = i2c_get_clientdata(i2c);
271
272         if (device_may_wakeup(dev))
273                 disable_irq_wake(max77686->irq);
274
275         max77686_irq_resume(max77686);
276         return 0;
277 }
278
279 const struct dev_pm_ops max77686_pm = {
280         .suspend = max77686_suspend,
281         .resume = max77686_resume,
282 };
283
284 static struct i2c_driver max77686_i2c_driver = {
285         .driver = {
286                    .name = "max77686",
287                    .owner = THIS_MODULE,
288                    .pm = &max77686_pm,
289                    .of_match_table = of_match_ptr(max77686_pmic_dt_match),
290                    },
291         .probe = max77686_i2c_probe,
292         .remove = max77686_i2c_remove,
293         .id_table = max77686_i2c_id,
294 };
295
296 static int __init max77686_i2c_init(void)
297 {
298         return i2c_add_driver(&max77686_i2c_driver);
299 }
300
301 subsys_initcall(max77686_i2c_init);
302
303 static void __exit max77686_i2c_exit(void)
304 {
305         i2c_del_driver(&max77686_i2c_driver);
306 }
307
308 module_exit(max77686_i2c_exit);
309
310 MODULE_DESCRIPTION("MAXIM 77686 multi-function core driver");
311 MODULE_AUTHOR("Chiwoong Byun <woong.byun@samsung.com>");
312 MODULE_LICENSE("GPL");