simple_open: automatically convert to simple_open()
[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 ssize_t aat2870_reg_read_file(struct file *file, char __user *user_buf,
261                                      size_t count, loff_t *ppos)
262 {
263         struct aat2870_data *aat2870 = file->private_data;
264         char *buf;
265         ssize_t ret;
266
267         buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
268         if (!buf)
269                 return -ENOMEM;
270
271         ret = aat2870_dump_reg(aat2870, buf);
272         if (ret >= 0)
273                 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
274
275         kfree(buf);
276
277         return ret;
278 }
279
280 static ssize_t aat2870_reg_write_file(struct file *file,
281                                       const char __user *user_buf, size_t count,
282                                       loff_t *ppos)
283 {
284         struct aat2870_data *aat2870 = file->private_data;
285         char buf[32];
286         ssize_t buf_size;
287         char *start = buf;
288         unsigned long addr, val;
289         int ret;
290
291         buf_size = min(count, (sizeof(buf)-1));
292         if (copy_from_user(buf, user_buf, buf_size)) {
293                 dev_err(aat2870->dev, "Failed to copy from user\n");
294                 return -EFAULT;
295         }
296         buf[buf_size] = 0;
297
298         while (*start == ' ')
299                 start++;
300
301         addr = simple_strtoul(start, &start, 16);
302         if (addr >= AAT2870_REG_NUM) {
303                 dev_err(aat2870->dev, "Invalid address, 0x%lx\n", addr);
304                 return -EINVAL;
305         }
306
307         while (*start == ' ')
308                 start++;
309
310         if (strict_strtoul(start, 16, &val))
311                 return -EINVAL;
312
313         ret = aat2870->write(aat2870, (u8)addr, (u8)val);
314         if (ret)
315                 return ret;
316
317         return buf_size;
318 }
319
320 static const struct file_operations aat2870_reg_fops = {
321         .open = simple_open,
322         .read = aat2870_reg_read_file,
323         .write = aat2870_reg_write_file,
324 };
325
326 static void aat2870_init_debugfs(struct aat2870_data *aat2870)
327 {
328         aat2870->dentry_root = debugfs_create_dir("aat2870", NULL);
329         if (!aat2870->dentry_root) {
330                 dev_warn(aat2870->dev,
331                          "Failed to create debugfs root directory\n");
332                 return;
333         }
334
335         aat2870->dentry_reg = debugfs_create_file("regs", 0644,
336                                                   aat2870->dentry_root,
337                                                   aat2870, &aat2870_reg_fops);
338         if (!aat2870->dentry_reg)
339                 dev_warn(aat2870->dev,
340                          "Failed to create debugfs register file\n");
341 }
342
343 static void aat2870_uninit_debugfs(struct aat2870_data *aat2870)
344 {
345         debugfs_remove_recursive(aat2870->dentry_root);
346 }
347 #else
348 static inline void aat2870_init_debugfs(struct aat2870_data *aat2870)
349 {
350 }
351
352 static inline void aat2870_uninit_debugfs(struct aat2870_data *aat2870)
353 {
354 }
355 #endif /* CONFIG_DEBUG_FS */
356
357 static int aat2870_i2c_probe(struct i2c_client *client,
358                              const struct i2c_device_id *id)
359 {
360         struct aat2870_platform_data *pdata = client->dev.platform_data;
361         struct aat2870_data *aat2870;
362         int i, j;
363         int ret = 0;
364
365         aat2870 = kzalloc(sizeof(struct aat2870_data), GFP_KERNEL);
366         if (!aat2870) {
367                 dev_err(&client->dev,
368                         "Failed to allocate memory for aat2870\n");
369                 ret = -ENOMEM;
370                 goto out;
371         }
372
373         aat2870->dev = &client->dev;
374         dev_set_drvdata(aat2870->dev, aat2870);
375
376         aat2870->client = client;
377         i2c_set_clientdata(client, aat2870);
378
379         aat2870->reg_cache = aat2870_regs;
380
381         if (pdata->en_pin < 0)
382                 aat2870->en_pin = -1;
383         else
384                 aat2870->en_pin = pdata->en_pin;
385
386         aat2870->init = pdata->init;
387         aat2870->uninit = pdata->uninit;
388         aat2870->read = aat2870_read;
389         aat2870->write = aat2870_write;
390         aat2870->update = aat2870_update;
391
392         mutex_init(&aat2870->io_lock);
393
394         if (aat2870->init)
395                 aat2870->init(aat2870);
396
397         if (aat2870->en_pin >= 0) {
398                 ret = gpio_request_one(aat2870->en_pin, GPIOF_OUT_INIT_HIGH,
399                                        "aat2870-en");
400                 if (ret < 0) {
401                         dev_err(&client->dev,
402                                 "Failed to request GPIO %d\n", aat2870->en_pin);
403                         goto out_kfree;
404                 }
405         }
406
407         aat2870_enable(aat2870);
408
409         for (i = 0; i < pdata->num_subdevs; i++) {
410                 for (j = 0; j < ARRAY_SIZE(aat2870_devs); j++) {
411                         if ((pdata->subdevs[i].id == aat2870_devs[j].id) &&
412                                         !strcmp(pdata->subdevs[i].name,
413                                                 aat2870_devs[j].name)) {
414                                 aat2870_devs[j].mfd_data =
415                                         pdata->subdevs[i].platform_data;
416                                 break;
417                         }
418                 }
419         }
420
421         ret = mfd_add_devices(aat2870->dev, 0, aat2870_devs,
422                               ARRAY_SIZE(aat2870_devs), NULL, 0);
423         if (ret != 0) {
424                 dev_err(aat2870->dev, "Failed to add subdev: %d\n", ret);
425                 goto out_disable;
426         }
427
428         aat2870_init_debugfs(aat2870);
429
430         return 0;
431
432 out_disable:
433         aat2870_disable(aat2870);
434         if (aat2870->en_pin >= 0)
435                 gpio_free(aat2870->en_pin);
436 out_kfree:
437         kfree(aat2870);
438 out:
439         return ret;
440 }
441
442 static int aat2870_i2c_remove(struct i2c_client *client)
443 {
444         struct aat2870_data *aat2870 = i2c_get_clientdata(client);
445
446         aat2870_uninit_debugfs(aat2870);
447
448         mfd_remove_devices(aat2870->dev);
449         aat2870_disable(aat2870);
450         if (aat2870->en_pin >= 0)
451                 gpio_free(aat2870->en_pin);
452         if (aat2870->uninit)
453                 aat2870->uninit(aat2870);
454         kfree(aat2870);
455
456         return 0;
457 }
458
459 #ifdef CONFIG_PM_SLEEP
460 static int aat2870_i2c_suspend(struct device *dev)
461 {
462         struct i2c_client *client = to_i2c_client(dev);
463         struct aat2870_data *aat2870 = i2c_get_clientdata(client);
464
465         aat2870_disable(aat2870);
466
467         return 0;
468 }
469
470 static int aat2870_i2c_resume(struct device *dev)
471 {
472         struct i2c_client *client = to_i2c_client(dev);
473         struct aat2870_data *aat2870 = i2c_get_clientdata(client);
474         struct aat2870_register *reg = NULL;
475         int i;
476
477         aat2870_enable(aat2870);
478
479         /* restore registers */
480         for (i = 0; i < AAT2870_REG_NUM; i++) {
481                 reg = &aat2870->reg_cache[i];
482                 if (reg->writeable)
483                         aat2870->write(aat2870, i, reg->value);
484         }
485
486         return 0;
487 }
488 #endif /* CONFIG_PM_SLEEP */
489
490 static SIMPLE_DEV_PM_OPS(aat2870_pm_ops, aat2870_i2c_suspend,
491                          aat2870_i2c_resume);
492
493 static const struct i2c_device_id aat2870_i2c_id_table[] = {
494         { "aat2870", 0 },
495         { }
496 };
497 MODULE_DEVICE_TABLE(i2c, aat2870_i2c_id_table);
498
499 static struct i2c_driver aat2870_i2c_driver = {
500         .driver = {
501                 .name   = "aat2870",
502                 .owner  = THIS_MODULE,
503                 .pm     = &aat2870_pm_ops,
504         },
505         .probe          = aat2870_i2c_probe,
506         .remove         = aat2870_i2c_remove,
507         .id_table       = aat2870_i2c_id_table,
508 };
509
510 static int __init aat2870_init(void)
511 {
512         return i2c_add_driver(&aat2870_i2c_driver);
513 }
514 subsys_initcall(aat2870_init);
515
516 static void __exit aat2870_exit(void)
517 {
518         i2c_del_driver(&aat2870_i2c_driver);
519 }
520 module_exit(aat2870_exit);
521
522 MODULE_DESCRIPTION("Core support for the AnalogicTech AAT2870");
523 MODULE_LICENSE("GPL");
524 MODULE_AUTHOR("Jin Park <jinyoungp@nvidia.com>");