]> nv-tegra.nvidia Code Review - linux-2.6.git/blob - drivers/mfd/aat2870-core.c
mfd: max8907c: Disable Hard Reset with Power key
[linux-2.6.git] / drivers / mfd / aat2870-core.c
1 /*
2  * linux/drivers/mfd/aat2870-core.c
3  *
4  * Copyright (c) 2011, NVIDIA Corporation.
5  * Author: Jin Park <jinyoungp@nvidia.com>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
19  * 02110-1301 USA
20  */
21
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/debugfs.h>
26 #include <linux/slab.h>
27 #include <linux/uaccess.h>
28 #include <linux/i2c.h>
29 #include <linux/delay.h>
30 #include <linux/gpio.h>
31 #include <linux/mfd/core.h>
32 #include <linux/mfd/aat2870.h>
33 #include <linux/regulator/machine.h>
34
35 static struct aat2870_register aat2870_regs[AAT2870_REG_NUM] = {
36         /* readable, writeable, value */
37         { 0, 1, 0x00 }, /* 0x00 AAT2870_BL_CH_EN */
38         { 0, 1, 0x16 }, /* 0x01 AAT2870_BLM */
39         { 0, 1, 0x16 }, /* 0x02 AAT2870_BLS */
40         { 0, 1, 0x56 }, /* 0x03 AAT2870_BL1 */
41         { 0, 1, 0x56 }, /* 0x04 AAT2870_BL2 */
42         { 0, 1, 0x56 }, /* 0x05 AAT2870_BL3 */
43         { 0, 1, 0x56 }, /* 0x06 AAT2870_BL4 */
44         { 0, 1, 0x56 }, /* 0x07 AAT2870_BL5 */
45         { 0, 1, 0x56 }, /* 0x08 AAT2870_BL6 */
46         { 0, 1, 0x56 }, /* 0x09 AAT2870_BL7 */
47         { 0, 1, 0x56 }, /* 0x0A AAT2870_BL8 */
48         { 0, 1, 0x00 }, /* 0x0B AAT2870_FLR */
49         { 0, 1, 0x03 }, /* 0x0C AAT2870_FM */
50         { 0, 1, 0x03 }, /* 0x0D AAT2870_FS */
51         { 0, 1, 0x10 }, /* 0x0E AAT2870_ALS_CFG0 */
52         { 0, 1, 0x06 }, /* 0x0F AAT2870_ALS_CFG1 */
53         { 0, 1, 0x00 }, /* 0x10 AAT2870_ALS_CFG2 */
54         { 1, 0, 0x00 }, /* 0x11 AAT2870_AMB */
55         { 0, 1, 0x00 }, /* 0x12 AAT2870_ALS0 */
56         { 0, 1, 0x00 }, /* 0x13 AAT2870_ALS1 */
57         { 0, 1, 0x00 }, /* 0x14 AAT2870_ALS2 */
58         { 0, 1, 0x00 }, /* 0x15 AAT2870_ALS3 */
59         { 0, 1, 0x00 }, /* 0x16 AAT2870_ALS4 */
60         { 0, 1, 0x00 }, /* 0x17 AAT2870_ALS5 */
61         { 0, 1, 0x00 }, /* 0x18 AAT2870_ALS6 */
62         { 0, 1, 0x00 }, /* 0x19 AAT2870_ALS7 */
63         { 0, 1, 0x00 }, /* 0x1A AAT2870_ALS8 */
64         { 0, 1, 0x00 }, /* 0x1B AAT2870_ALS9 */
65         { 0, 1, 0x00 }, /* 0x1C AAT2870_ALSA */
66         { 0, 1, 0x00 }, /* 0x1D AAT2870_ALSB */
67         { 0, 1, 0x00 }, /* 0x1E AAT2870_ALSC */
68         { 0, 1, 0x00 }, /* 0x1F AAT2870_ALSD */
69         { 0, 1, 0x00 }, /* 0x20 AAT2870_ALSE */
70         { 0, 1, 0x00 }, /* 0x21 AAT2870_ALSF */
71         { 0, 1, 0x00 }, /* 0x22 AAT2870_SUB_SET */
72         { 0, 1, 0x00 }, /* 0x23 AAT2870_SUB_CTRL */
73         { 0, 1, 0x00 }, /* 0x24 AAT2870_LDO_AB */
74         { 0, 1, 0x00 }, /* 0x25 AAT2870_LDO_CD */
75         { 0, 1, 0x00 }, /* 0x26 AAT2870_LDO_EN */
76 };
77
78 static struct mfd_cell aat2870_devs[] = {
79         {
80                 .name = "aat2870-backlight",
81                 .id = AAT2870_ID_BL,
82         },
83         {
84                 .name = "aat2870-regulator",
85                 .id = AAT2870_ID_LDOA,
86         },
87         {
88                 .name = "aat2870-regulator",
89                 .id = AAT2870_ID_LDOB,
90         },
91         {
92                 .name = "aat2870-regulator",
93                 .id = AAT2870_ID_LDOC,
94         },
95         {
96                 .name = "aat2870-regulator",
97                 .id = AAT2870_ID_LDOD,
98         },
99 };
100
101 static int __aat2870_read(struct aat2870_data *aat2870, u8 addr, u8 *val)
102 {
103         int ret;
104
105         if (addr >= AAT2870_REG_NUM) {
106                 dev_err(aat2870->dev, "Invalid address, 0x%02x\n", addr);
107                 return -EINVAL;
108         }
109
110         if (!aat2870->reg_cache[addr].readable) {
111                 *val = aat2870->reg_cache[addr].value;
112                 goto out;
113         }
114
115         ret = i2c_master_send(aat2870->client, &addr, 1);
116         if (ret < 0)
117                 return ret;
118         if (ret != 1)
119                 return -EIO;
120
121         ret = i2c_master_recv(aat2870->client, val, 1);
122         if (ret < 0)
123                 return ret;
124         if (ret != 1)
125                 return -EIO;
126
127 out:
128         dev_dbg(aat2870->dev, "read: addr=0x%02x, val=0x%02x\n", addr, *val);
129         return 0;
130 }
131
132 static int __aat2870_write(struct aat2870_data *aat2870, u8 addr, u8 val)
133 {
134         u8 msg[2];
135         int ret;
136
137         if (addr >= AAT2870_REG_NUM) {
138                 dev_err(aat2870->dev, "Invalid address, 0x%02x\n", addr);
139                 return -EINVAL;
140         }
141
142         if (!aat2870->reg_cache[addr].writeable) {
143                 dev_err(aat2870->dev, "Address 0x%02x is not writeable\n",
144                         addr);
145                 return -EINVAL;
146         }
147
148         msg[0] = addr;
149         msg[1] = val;
150         ret = i2c_master_send(aat2870->client, msg, 2);
151         if (ret < 0)
152                 return ret;
153         if (ret != 2)
154                 return -EIO;
155
156         aat2870->reg_cache[addr].value = val;
157
158         dev_dbg(aat2870->dev, "write: addr=0x%02x, val=0x%02x\n", addr, val);
159         return 0;
160 }
161
162 static int aat2870_read(struct aat2870_data *aat2870, u8 addr, u8 *val)
163 {
164         int ret;
165
166         mutex_lock(&aat2870->io_lock);
167         ret = __aat2870_read(aat2870, addr, val);
168         mutex_unlock(&aat2870->io_lock);
169
170         return ret;
171 }
172
173 static int aat2870_write(struct aat2870_data *aat2870, u8 addr, u8 val)
174 {
175         int ret;
176
177         mutex_lock(&aat2870->io_lock);
178         ret = __aat2870_write(aat2870, addr, val);
179         mutex_unlock(&aat2870->io_lock);
180
181         return ret;
182 }
183
184 static int aat2870_update(struct aat2870_data *aat2870, u8 addr, u8 mask,
185                           u8 val)
186 {
187         int change;
188         u8 old_val, new_val;
189         int ret;
190
191         mutex_lock(&aat2870->io_lock);
192
193         ret = __aat2870_read(aat2870, addr, &old_val);
194         if (ret)
195                 goto out_unlock;
196
197         new_val = (old_val & ~mask) | (val & mask);
198         change = old_val != new_val;
199         if (change)
200                 ret = __aat2870_write(aat2870, addr, new_val);
201
202 out_unlock:
203         mutex_unlock(&aat2870->io_lock);
204
205         return ret;
206 }
207
208 static inline void aat2870_enable(struct aat2870_data *aat2870)
209 {
210         if (aat2870->en_pin >= 0)
211                 gpio_set_value(aat2870->en_pin, 1);
212
213         aat2870->is_enable = 1;
214 }
215
216 static inline void aat2870_disable(struct aat2870_data *aat2870)
217 {
218         if (aat2870->en_pin >= 0)
219                 gpio_set_value(aat2870->en_pin, 0);
220
221         aat2870->is_enable = 0;
222 }
223
224 #ifdef CONFIG_DEBUG_FS
225 static ssize_t aat2870_dump_reg(struct aat2870_data *aat2870, char *buf)
226 {
227         u8 addr, val;
228         ssize_t count = 0;
229         int ret;
230
231         count += sprintf(buf, "aat2870 registers\n");
232         for (addr = 0; addr < AAT2870_REG_NUM; addr++) {
233                 count += sprintf(buf + count, "0x%02x: ", addr);
234                 if (count >= PAGE_SIZE - 1)
235                         break;
236
237                 ret = aat2870->read(aat2870, addr, &val);
238                 if (ret == 0)
239                         count += snprintf(buf + count, PAGE_SIZE - count,
240                                           "0x%02x", val);
241                 else
242                         count += snprintf(buf + count, PAGE_SIZE - count,
243                                           "<read fail: %d>", ret);
244
245                 if (count >= PAGE_SIZE - 1)
246                         break;
247
248                 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
249                 if (count >= PAGE_SIZE - 1)
250                         break;
251         }
252
253         /* Truncate count; min() would cause a warning */
254         if (count >= PAGE_SIZE)
255                 count = PAGE_SIZE - 1;
256
257         return count;
258 }
259
260 static int aat2870_reg_open_file(struct inode *inode, struct file *file)
261 {
262         file->private_data = inode->i_private;
263
264         return 0;
265 }
266
267 static ssize_t aat2870_reg_read_file(struct file *file, char __user *user_buf,
268                                      size_t count, loff_t *ppos)
269 {
270         struct aat2870_data *aat2870 = file->private_data;
271         char *buf;
272         ssize_t ret;
273
274         buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
275         if (!buf)
276                 return -ENOMEM;
277
278         ret = aat2870_dump_reg(aat2870, buf);
279         if (ret >= 0)
280                 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
281
282         kfree(buf);
283
284         return ret;
285 }
286
287 static ssize_t aat2870_reg_write_file(struct file *file,
288                                       const char __user *user_buf, size_t count,
289                                       loff_t *ppos)
290 {
291         struct aat2870_data *aat2870 = file->private_data;
292         char buf[32];
293         int buf_size;
294         char *start = buf;
295         unsigned long addr, val;
296         int ret;
297
298         buf_size = min(count, (sizeof(buf)-1));
299         if (copy_from_user(buf, user_buf, buf_size)) {
300                 dev_err(aat2870->dev, "Failed to copy from user\n");
301                 return -EFAULT;
302         }
303         buf[buf_size] = 0;
304
305         while (*start == ' ')
306                 start++;
307
308         addr = simple_strtoul(start, &start, 16);
309         if (addr >= AAT2870_REG_NUM) {
310                 dev_err(aat2870->dev, "Invalid address, 0x%lx\n", addr);
311                 return -EINVAL;
312         }
313
314         while (*start == ' ')
315                 start++;
316
317         if (strict_strtoul(start, 16, &val))
318                 return -EINVAL;
319
320         ret = aat2870->write(aat2870, (u8)addr, (u8)val);
321         if (ret)
322                 return ret;
323
324         return buf_size;
325 }
326
327 static const struct file_operations aat2870_reg_fops = {
328         .open = aat2870_reg_open_file,
329         .read = aat2870_reg_read_file,
330         .write = aat2870_reg_write_file,
331 };
332
333 static void aat2870_init_debugfs(struct aat2870_data *aat2870)
334 {
335         aat2870->dentry_root = debugfs_create_dir("aat2870", NULL);
336         if (!aat2870->dentry_root) {
337                 dev_warn(aat2870->dev,
338                          "Failed to create debugfs root directory\n");
339                 return;
340         }
341
342         aat2870->dentry_reg = debugfs_create_file("regs", 0644,
343                                                   aat2870->dentry_root,
344                                                   aat2870, &aat2870_reg_fops);
345         if (!aat2870->dentry_reg)
346                 dev_warn(aat2870->dev,
347                          "Failed to create debugfs register file\n");
348 }
349
350 static void aat2870_uninit_debugfs(struct aat2870_data *aat2870)
351 {
352         debugfs_remove_recursive(aat2870->dentry_root);
353 }
354 #else
355 static inline void aat2870_init_debugfs(struct aat2870_data *aat2870)
356 {
357 }
358
359 static inline void aat2870_uninit_debugfs(struct aat2870_data *aat2870)
360 {
361 }
362 #endif /* CONFIG_DEBUG_FS */
363
364 static int aat2870_i2c_probe(struct i2c_client *client,
365                              const struct i2c_device_id *id)
366 {
367         struct aat2870_platform_data *pdata = client->dev.platform_data;
368         struct aat2870_data *aat2870;
369         int i, j;
370         int ret = 0;
371
372         aat2870 = kzalloc(sizeof(struct aat2870_data), GFP_KERNEL);
373         if (!aat2870) {
374                 dev_err(&client->dev,
375                         "Failed to allocate memory for aat2870\n");
376                 ret = -ENOMEM;
377                 goto out;
378         }
379
380         aat2870->dev = &client->dev;
381         dev_set_drvdata(aat2870->dev, aat2870);
382
383         aat2870->client = client;
384         i2c_set_clientdata(client, aat2870);
385
386         aat2870->reg_cache = aat2870_regs;
387
388         if (pdata->en_pin < 0)
389                 aat2870->en_pin = -1;
390         else
391                 aat2870->en_pin = pdata->en_pin;
392
393         aat2870->init = pdata->init;
394         aat2870->uninit = pdata->uninit;
395         aat2870->read = aat2870_read;
396         aat2870->write = aat2870_write;
397         aat2870->update = aat2870_update;
398
399         mutex_init(&aat2870->io_lock);
400
401         if (aat2870->init)
402                 aat2870->init(aat2870);
403
404         if (aat2870->en_pin >= 0) {
405                 ret = gpio_request(aat2870->en_pin, "aat2870-en");
406                 if (ret < 0) {
407                         dev_err(&client->dev,
408                                 "Failed to request GPIO %d\n", aat2870->en_pin);
409                         goto out_kfree;
410                 }
411                 gpio_direction_output(aat2870->en_pin, 1);
412         }
413
414         aat2870_enable(aat2870);
415
416         for (i = 0; i < pdata->num_subdevs; i++) {
417                 for (j = 0; j < ARRAY_SIZE(aat2870_devs); j++) {
418                         if ((pdata->subdevs[i].id == aat2870_devs[j].id) &&
419                                         !strcmp(pdata->subdevs[i].name,
420                                                 aat2870_devs[j].name)) {
421                                 aat2870_devs[j].mfd_data =
422                                         pdata->subdevs[i].platform_data;
423                                 break;
424                         }
425                 }
426         }
427
428         ret = mfd_add_devices(aat2870->dev, 0, aat2870_devs,
429                               ARRAY_SIZE(aat2870_devs), NULL, 0);
430         if (ret != 0) {
431                 dev_err(aat2870->dev, "Failed to add subdev: %d\n", ret);
432                 goto out_disable;
433         }
434
435         aat2870_init_debugfs(aat2870);
436
437         return 0;
438
439 out_disable:
440         aat2870_disable(aat2870);
441         if (aat2870->en_pin >= 0)
442                 gpio_free(aat2870->en_pin);
443 out_kfree:
444         kfree(aat2870);
445 out:
446         return ret;
447 }
448
449 static int aat2870_i2c_remove(struct i2c_client *client)
450 {
451         struct aat2870_data *aat2870 = i2c_get_clientdata(client);
452
453         aat2870_uninit_debugfs(aat2870);
454
455         mfd_remove_devices(aat2870->dev);
456         aat2870_disable(aat2870);
457         if (aat2870->en_pin >= 0)
458                 gpio_free(aat2870->en_pin);
459         if (aat2870->uninit)
460                 aat2870->uninit(aat2870);
461         kfree(aat2870);
462
463         return 0;
464 }
465
466 #ifdef CONFIG_PM
467 static int aat2870_i2c_suspend(struct i2c_client *client, pm_message_t state)
468 {
469         struct aat2870_data *aat2870 = i2c_get_clientdata(client);
470
471         aat2870_disable(aat2870);
472
473         return 0;
474 }
475
476 static int aat2870_i2c_resume(struct i2c_client *client)
477 {
478         struct aat2870_data *aat2870 = i2c_get_clientdata(client);
479         struct aat2870_register *reg = NULL;
480         int i;
481
482         aat2870_enable(aat2870);
483
484         /* restore registers */
485         for (i = 0; i < AAT2870_REG_NUM; i++) {
486                 reg = &aat2870->reg_cache[i];
487                 if (reg->writeable)
488                         aat2870->write(aat2870, i, reg->value);
489         }
490
491         return 0;
492 }
493 #else
494 #define aat2870_i2c_suspend     NULL
495 #define aat2870_i2c_resume      NULL
496 #endif /* CONFIG_PM */
497
498 static struct i2c_device_id aat2870_i2c_id_table[] = {
499         { "aat2870", 0 },
500         { }
501 };
502 MODULE_DEVICE_TABLE(i2c, aat2870_i2c_id_table);
503
504 static struct i2c_driver aat2870_i2c_driver = {
505         .driver = {
506                 .name   = "aat2870",
507                 .owner  = THIS_MODULE,
508         },
509         .probe          = aat2870_i2c_probe,
510         .remove         = aat2870_i2c_remove,
511         .suspend        = aat2870_i2c_suspend,
512         .resume         = aat2870_i2c_resume,
513         .id_table       = aat2870_i2c_id_table,
514 };
515
516 static int __init aat2870_init(void)
517 {
518         return i2c_add_driver(&aat2870_i2c_driver);
519 }
520 subsys_initcall(aat2870_init);
521
522 static void __exit aat2870_exit(void)
523 {
524         i2c_del_driver(&aat2870_i2c_driver);
525 }
526 module_exit(aat2870_exit);
527
528 MODULE_DESCRIPTION("Core support for the AnalogicTech AAT2870");
529 MODULE_LICENSE("GPL");
530 MODULE_AUTHOR("Jin Park <jinyoungp@nvidia.com>");