c1093905152351b1272fb7451bd3d273d4d93790
[linux-2.6.git] / drivers / mfd / max77665.c
1 /*
2  * Core driver for MAXIM MAX77665
3  *
4  * Copyright (c) 2012, NVIDIA CORPORATION.  All rights reserved.
5
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include <linux/err.h>
20 #include <linux/i2c.h>
21 #include <linux/interrupt.h>
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/mutex.h>
25 #include <linux/mfd/core.h>
26 #include <linux/mfd/max77665.h>
27 #include <linux/slab.h>
28
29 #define MAX77665_INT_STS        0x22
30 #define MAX77665_INT_MSK        0x23
31 #define MAX77665_PMIC_FLASH     0x00 ... 0x10
32 #define MAX77665_PMIC_PMIC      0x20 ... 0x2D
33 #define MAX77665_PMIC_CHARGER   0xB0 ... 0xC6
34 #define MAX77665_MUIC           0x00 ... 0x0E
35 #define MAX77665_HAPTIC         0x00 ... 0x10
36
37 static u8 max77665_i2c_slave_address[] = {
38         [MAX77665_I2C_SLAVE_PMIC] = 0x66,
39         [MAX77665_I2C_SLAVE_MUIC] = 0x25,
40         [MAX77665_I2C_SLAVE_HAPTIC] = 0x48,
41 };
42
43 static const struct resource charger_resource[] = {
44         {
45                 .start = MAX77665_IRQ_CHARGER,
46                 .end = MAX77665_IRQ_CHARGER,
47                 .flags = IORESOURCE_IRQ,
48         },
49 };
50
51 static const struct resource flash_resource[] = {
52         {
53                 .start = MAX77665_IRQ_FLASH,
54                 .end = MAX77665_IRQ_FLASH,
55                 .flags = IORESOURCE_IRQ,
56         },
57 };
58
59 static const struct resource muic_resource[] = {
60         {
61                 .start = MAX77665_IRQ_MUIC,
62                 .end = MAX77665_IRQ_MUIC,
63                 .flags = IORESOURCE_IRQ,
64         },
65 };
66
67 static struct mfd_cell max77665s[] = {
68         [MAX77665_CELL_CHARGER] = {
69                         .name = "max77665-charger",
70                         .resources = charger_resource,
71                         .num_resources = ARRAY_SIZE(charger_resource),
72         },
73
74         [MAX77665_CELL_FLASH]   = {
75                         .name = "max77665-flash",
76                         .resources = flash_resource,
77                         .num_resources = ARRAY_SIZE(flash_resource),
78         },
79
80         [MAX77665_CELL_MUIC]    = {
81                         .name = "max77665-muic",
82                         .resources = muic_resource,
83                         .num_resources = ARRAY_SIZE(muic_resource),
84         },
85
86         [MAX77665_CELL_HAPTIC]  = {
87                         .name = "max77665-haptic",
88         },
89 };
90
91 static const struct regmap_irq max77665_irqs[] = {
92         [MAX77665_IRQ_CHARGER] = {
93                 .mask = MAX77665_INTSRC_CHGR_MASK,
94         },
95         [MAX77665_IRQ_TOP_SYS] = {
96                 .mask = MAX77665_INTSRC_TOP_MASK,
97         },
98         [MAX77665_IRQ_FLASH] = {
99                 .mask = MAX77665_INTSRC_FLASH_MASK,
100         },
101         [MAX77665_IRQ_MUIC] = {
102                 .mask = MAX77665_INTSRC_MUIC_MASK,
103         },
104 };
105
106 static struct regmap_irq_chip max77665_irq_chip = {
107         .name = "max77665-irq",
108         .irqs = max77665_irqs,
109         .num_irqs = ARRAY_SIZE(max77665_irqs),
110         .num_regs = 1,
111         .irq_reg_stride = 1,
112         .status_base = MAX77665_REG_INTSRC,
113         .mask_base = MAX77665_REG_INTSRC_MASK,
114 };
115
116 static int max77665_irq_init(struct max77665 *max77665, int irq,
117         int irq_base, unsigned long irq_flag)
118 {
119         int ret;
120
121         if (!irq || (irq_base <= 0)) {
122                 dev_err(max77665->dev, "IRQ base not set, int not supported\n");
123                 return -EINVAL;
124         }
125
126         ret = regmap_add_irq_chip(max77665->regmap[MAX77665_I2C_SLAVE_PMIC],
127                         irq, irq_flag | IRQF_ONESHOT, irq_base,
128                         &max77665_irq_chip, &max77665->regmap_irq_data);
129         if (ret < 0)
130                 dev_err(max77665->dev, "regmap irq registration failed %d\n",
131                         ret);
132         return ret;
133 }
134
135 static bool rd_wr_reg_pmic(struct device *dev, unsigned int reg)
136 {
137         switch (reg) {
138         case MAX77665_PMIC_FLASH:
139         case MAX77665_PMIC_PMIC:
140         case MAX77665_PMIC_CHARGER:
141                 return true;
142         default:
143                 dev_dbg(dev, "non-existing reg %s() reg 0x%x\n", __func__, reg);
144                 return false;
145         }
146 }
147
148 static bool rd_wr_reg_muic(struct device *dev, unsigned int reg)
149 {
150         switch (reg) {
151         case MAX77665_MUIC:
152                 return true;
153         default:
154                 dev_dbg(dev, "non-existing reg %s() reg 0x%x\n", __func__, reg);
155                 return false;
156         }
157 }
158
159 static bool rd_wr_reg_haptic(struct device *dev, unsigned int reg)
160 {
161         switch (reg) {
162         case MAX77665_HAPTIC:
163                 return true;
164         default:
165                 dev_dbg(dev, "non-existing reg %s() reg 0x%x\n", __func__, reg);
166                 return false;
167         }
168 }
169
170 static const struct regmap_config max77665_regmap_config[] = {
171         {
172                 .reg_bits = 8,
173                 .val_bits = 8,
174                 .max_register = 0xFF,
175                 .writeable_reg = rd_wr_reg_pmic,
176                 .readable_reg = rd_wr_reg_pmic,
177                 .cache_type = REGCACHE_RBTREE,
178         }, {
179                 .reg_bits = 8,
180                 .val_bits = 8,
181                 .max_register = 0x0E,
182                 .writeable_reg = rd_wr_reg_muic,
183                 .readable_reg = rd_wr_reg_muic,
184                 .cache_type = REGCACHE_RBTREE,
185         }, {
186                 .reg_bits = 8,
187                 .val_bits = 8,
188                 .max_register = 0x10,
189                 .writeable_reg = rd_wr_reg_haptic,
190                 .readable_reg = rd_wr_reg_haptic,
191                 .cache_type = REGCACHE_RBTREE,
192         },
193 };
194
195 static int __devinit max77665_i2c_probe(struct i2c_client *client,
196                                         const struct i2c_device_id *id)
197 {
198         struct max77665_platform_data *pdata = client->dev.platform_data;
199         struct max77665 *max77665;
200         struct i2c_client *slv_client;
201         int ret;
202         int i;
203
204         if (!pdata) {
205                 dev_err(&client->dev, "max77665 requires platform data\n");
206                 return -EINVAL;
207         }
208
209         max77665 = devm_kzalloc(&client->dev, sizeof(*max77665), GFP_KERNEL);
210         if (!max77665) {
211                 dev_err(&client->dev, "mem alloc for max77665 failed\n");
212                 return -ENOMEM;
213         }
214
215         max77665->dev = &client->dev;
216
217         for (i = 0; i < MAX77665_I2C_SLAVE_MAX; ++i) {
218                 if (i == 0)
219                         slv_client = client;
220                 else
221                         slv_client = i2c_new_dummy(client->adapter,
222                                                 max77665_i2c_slave_address[i]);
223                 if (!slv_client) {
224                         dev_err(&client->dev, "can't attach client %d\n", i);
225                         ret = -ENOMEM;
226                         goto err_exit;
227                 }
228                 max77665->client[i] = slv_client;
229                 i2c_set_clientdata(slv_client, max77665);
230
231                 max77665->regmap[i] = devm_regmap_init_i2c(slv_client,
232                                         &max77665_regmap_config[i]);
233                 if (IS_ERR(max77665->regmap[i])) {
234                         ret = PTR_ERR(max77665->regmap[i]);
235                         dev_err(&client->dev,
236                                 "regmap %d init failed with err: %d\n", i, ret);
237                         goto err_exit;
238                 }
239         }
240
241         max77665_irq_init(max77665, client->irq,
242                                 pdata->irq_base, pdata->irq_flag);
243
244         max77665s[MAX77665_CELL_CHARGER].platform_data =
245                                         pdata->charger_platform_data.pdata;
246         max77665s[MAX77665_CELL_CHARGER].pdata_size =
247                                         pdata->charger_platform_data.size;
248
249         max77665s[MAX77665_CELL_FLASH].platform_data =
250                                         pdata->flash_platform_data.pdata;
251         max77665s[MAX77665_CELL_FLASH].pdata_size =
252                                         pdata->flash_platform_data.size;
253
254         max77665s[MAX77665_CELL_MUIC].platform_data =
255                                         pdata->muic_platform_data.pdata;
256         max77665s[MAX77665_CELL_MUIC].pdata_size =
257                                         pdata->muic_platform_data.size;
258
259         max77665s[MAX77665_CELL_HAPTIC].platform_data =
260                                         pdata->haptic_platform_data.pdata;
261         max77665s[MAX77665_CELL_HAPTIC].pdata_size =
262                                         pdata->haptic_platform_data.size;
263
264         ret = mfd_add_devices(max77665->dev, -1, max77665s,
265                 ARRAY_SIZE(max77665s), NULL, pdata->irq_base);
266         if (ret) {
267                 dev_err(&client->dev, "add mfd devices failed with err: %d\n",
268                         ret);
269                 goto err_irq_exit;
270         }
271
272         return 0;
273
274 err_irq_exit:
275         regmap_del_irq_chip(client->irq, max77665->regmap_irq_data);
276
277 err_exit:
278         for (i = 0; i < MAX77665_I2C_SLAVE_MAX; ++i) {
279                 slv_client = max77665->client[i];
280                 if (slv_client && slv_client != client)
281                         i2c_unregister_device(slv_client);
282         }
283         return ret;
284 }
285
286 static int __devexit max77665_i2c_remove(struct i2c_client *client)
287 {
288         struct max77665 *max77665 = i2c_get_clientdata(client);
289         int i;
290         struct i2c_client *slv_client;
291
292         mfd_remove_devices(max77665->dev);
293         regmap_del_irq_chip(client->irq, max77665->regmap_irq_data);
294
295         for (i = 0; i < MAX77665_I2C_SLAVE_MAX; ++i) {
296                 slv_client = max77665->client[i];
297                 if (slv_client && slv_client != client)
298                         i2c_unregister_device(slv_client);
299         }
300
301         return 0;
302 }
303
304 static const struct i2c_device_id max77665_id_table[] = {
305         { "max77665", 0 },
306         { },
307 };
308 MODULE_DEVICE_TABLE(i2c, max77665_id_table);
309
310 static struct i2c_driver max77665_driver = {
311         .driver = {
312                 .name   = "max77665",
313                 .owner  = THIS_MODULE,
314         },
315         .probe          = max77665_i2c_probe,
316         .remove         = __devexit_p(max77665_i2c_remove),
317         .id_table       = max77665_id_table,
318 };
319
320 static int __init max77665_init(void)
321 {
322         return i2c_add_driver(&max77665_driver);
323 }
324 subsys_initcall(max77665_init);
325
326 static void __exit max77665_exit(void)
327 {
328         i2c_del_driver(&max77665_driver);
329 }
330 module_exit(max77665_exit);
331
332 MODULE_DESCRIPTION("MAXIM MAX77665 core driver");
333 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
334 MODULE_LICENSE("GPL v2");