temp revert rk change
[firefly-linux-kernel-4.4.55.git] / drivers / misc / max9635.c
1 /*
2  * Copyright (C) 2010 Motorola, Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
16  * 02111-1307, USA
17  */
18
19 #include <linux/delay.h>
20 #include <linux/i2c.h>
21 #include <linux/irq.h>
22 #include <linux/input.h>
23 #include <linux/interrupt.h>
24 #include <linux/leds.h>
25 #include <linux/max9635.h>
26 #include <linux/miscdevice.h>
27 #include <linux/platform_device.h>
28 #include <linux/regulator/consumer.h>
29 #include <linux/slab.h>
30 #include <linux/types.h>
31 #include <linux/uaccess.h>
32 #include <linux/workqueue.h>
33 #include <linux/spinlock.h>
34
35 #define DEBUG   1
36
37 #define MAX9635_ALLOWED_R_BYTES 1
38 #define MAX9635_ALLOWED_W_BYTES 2
39 #define MAX9635_MAX_RW_RETRIES 5
40 #define MAX9635_I2C_RETRY_DELAY 10
41 #define AUTO_INCREMENT          0x0
42
43 #define MAX9635_INT_STATUS      0x00
44 #define MAX9635_INT_EN          0x01
45 #define MAX9635_CONFIGURE       0x02
46 #define MAX9635_ALS_DATA_H      0x03
47 #define MAX9635_ALS_DATA_L      0x04
48 #define MAX9635_ALS_THRESH_H    0x05
49 #define MAX9635_ALS_THRESH_L    0x06
50 #define MAX9635_THRESH_TIMER    0x07
51
52 struct max9635_zone_conv {
53         int lower_threshold;
54         int upper_threshold;
55 };
56
57 struct max9635_data {
58         struct input_dev *idev;
59         struct i2c_client *client;
60         struct delayed_work working_queue;
61         struct max9635_platform_data *als_pdata;
62         struct max9635_zone_conv max9635_zone_info[255];
63         atomic_t enabled;
64         spinlock_t irq_lock;
65         int cur_irq_state;
66         struct regulator *regulator;
67 };
68
69 struct max9635_data *max9635_misc_data;
70
71 #ifdef DEBUG
72 struct max9635_reg {
73         const char *name;
74         uint8_t reg;
75 } max9635_regs[] = {
76         {"INT_STATUS",          MAX9635_INT_STATUS},
77         {"INT_ENABLE",          MAX9635_INT_EN},
78         {"CONFIG",              MAX9635_CONFIGURE},
79         {"ALS_DATA_HIGH",       MAX9635_ALS_DATA_H},
80         {"ALS_DATA_LOW",        MAX9635_ALS_DATA_L},
81         {"ALS_THRESH_H",        MAX9635_ALS_THRESH_H},
82         {"ALS_THRESH_L",        MAX9635_ALS_THRESH_L},
83         {"ALS_THRESH_TIMER",    MAX9635_THRESH_TIMER},
84 };
85 #endif
86
87 static uint32_t max9635_debug = 0x00;
88 module_param_named(als_debug, max9635_debug, uint, 0664);
89
90 static int max9635_read_reg(struct max9635_data *als_data, u8 * buf, int len)
91 {
92         int err;
93         int tries = 0;
94         struct i2c_msg msgs[] = {
95                 {
96                  .addr = als_data->client->addr,
97                  .flags = als_data->client->flags & I2C_M_TEN,
98                  .len = 1,
99                  .buf = buf,
100                  },
101                 {
102                  .addr = als_data->client->addr,
103                  .flags = (als_data->client->flags & I2C_M_TEN) | I2C_M_RD,
104                  .len = len,
105                  .buf = buf,
106                  },
107         };
108
109         do {
110                 err = i2c_transfer(als_data->client->adapter, msgs, 2);
111                 if (err != 2)
112                         msleep_interruptible(MAX9635_I2C_RETRY_DELAY);
113         } while ((err != 2) && (++tries < MAX9635_MAX_RW_RETRIES));
114
115         if (err != 2) {
116                 pr_err("%s:read transfer error\n", __func__);
117                 err = -EIO;
118         } else {
119                 err = 0;
120         }
121
122         return err;
123 }
124
125 static int max9635_write_reg(struct max9635_data *als_data, u8 * buf, int len)
126 {
127         int err;
128         int tries = 0;
129         struct i2c_msg msgs[] = {
130                 {
131                  .addr = als_data->client->addr,
132                  .flags = als_data->client->flags & I2C_M_TEN,
133                  .len = len + 1,
134                  .buf = buf,
135                  },
136         };
137
138         do {
139                 err = i2c_transfer(als_data->client->adapter, msgs, 1);
140                 if (err != 1)
141                         msleep_interruptible(MAX9635_I2C_RETRY_DELAY);
142         } while ((err != 1) && (++tries < MAX9635_MAX_RW_RETRIES));
143
144         if (err != 1) {
145                 pr_err("%s:write transfer error\n", __func__);
146                 err = -EIO;
147         } else {
148                 err = 0;
149         }
150
151         return err;
152 }
153
154 static int max9635_init_registers(struct max9635_data *als_data)
155 {
156         u8 buf[2];
157
158         buf[0] = (AUTO_INCREMENT | MAX9635_CONFIGURE);
159         buf[1] = als_data->als_pdata->configure;
160         if (max9635_write_reg(als_data, buf, 1))
161                 goto init_failed;
162
163         buf[0] = (AUTO_INCREMENT | MAX9635_ALS_THRESH_H);
164         buf[1] = als_data->als_pdata->def_high_threshold;
165         if (max9635_write_reg(als_data, buf, 1))
166                 goto init_failed;
167
168         buf[0] = (AUTO_INCREMENT | MAX9635_ALS_THRESH_L);
169         buf[1] = als_data->als_pdata->def_low_threshold;
170         if (max9635_write_reg(als_data, buf, 1))
171                 goto init_failed;
172
173         buf[0] = (AUTO_INCREMENT | MAX9635_THRESH_TIMER);
174         buf[1] = als_data->als_pdata->threshold_timer;
175         if (max9635_write_reg(als_data, buf, 1))
176                 goto init_failed;
177
178         return 0;
179
180 init_failed:
181         pr_err("%s:Register 0x%d initialization failed\n", __func__, buf[0]);
182         return -EINVAL;
183 }
184
185 static void max9635_irq_enable(struct max9635_data *als_data, int enable)
186 {
187         unsigned long flags;
188
189         spin_lock_irqsave(&als_data->irq_lock, flags);
190         if (als_data->cur_irq_state != enable) {
191                 if (enable)
192                         enable_irq(als_data->client->irq);
193                 else
194                         disable_irq_nosync(als_data->client->irq);
195                 als_data->cur_irq_state = enable;
196         }
197         spin_unlock_irqrestore(&als_data->irq_lock, flags);
198 }
199
200 static irqreturn_t max9635_irq_handler(int irq, void *dev)
201 {
202         struct max9635_data *als_data = dev;
203
204         max9635_irq_enable(als_data, 0);
205         schedule_delayed_work(&als_data->working_queue, 0);
206
207         return IRQ_HANDLED;
208 }
209
210 static int max9635_read_adj_als(struct max9635_data *als_data)
211 {
212         int ret;
213         int lux = 0;
214         u8 low_buf = MAX9635_ALS_DATA_L;
215         u8 high_buf = MAX9635_ALS_DATA_H;
216         u8 exponent;
217         u16 mantissa;
218
219         ret = max9635_read_reg(als_data, &high_buf, 1);
220         if (ret != 0) {
221                 pr_err("%s: Unable to read lux high byte register: %d\n",
222                        __func__, ret);
223                 return -1;
224         }
225         ret = max9635_read_reg(als_data, &low_buf, 1);
226         if (ret != 0) {
227                 pr_err("%s: Unable to read lux low byte register: %d\n",
228                        __func__, ret);
229                 return -1;
230         }
231
232         exponent = (high_buf & 0xf0) >> 4;
233         mantissa = ((high_buf & 0x0f) << 4) | (low_buf & 0x0f);
234
235         lux = ((0001 << exponent) * mantissa) / 20;
236         if (max9635_debug & 1)
237                 pr_info("exp = 0x%X, mant = 0x%X, lux = %d\n",
238                         exponent, mantissa, lux);
239
240         lux = lux * (exponent + als_data->als_pdata->lens_coeff);
241
242         if (max9635_debug & 1)
243                 pr_info("%s:Reporting LUX %d\n", __func__, lux);
244         return lux;
245 }
246
247 static int max9635_report_input(struct max9635_data *als_data)
248 {
249         int ret = 0;
250         int lux_val;
251         u8 buf[2] = { MAX9635_INT_STATUS, 0x00 };
252
253         lux_val = max9635_read_adj_als(als_data);
254         if (lux_val >= 0) {
255                 input_event(als_data->idev, EV_MSC, MSC_RAW, lux_val);
256                 input_sync(als_data->idev);
257         }
258
259         /* Clear the interrupt status register */
260         ret = max9635_read_reg(als_data, buf, 1);
261         if (ret != 0) {
262                 pr_err("%s:Unable to read interrupt register: %d\n",
263                        __func__, ret);
264                 return -1;
265         }
266         max9635_irq_enable(als_data, 1);
267         return ret;
268 }
269
270 static int max9635_device_power(struct max9635_data *als_data, u8 state)
271 {
272         int err;
273         u8 buf[2] = { (AUTO_INCREMENT | MAX9635_INT_EN) };
274
275         buf[1] = state;
276         err = max9635_write_reg(als_data, buf, 1);
277         if (err)
278                 pr_err("%s:Unable to turn off prox: %d\n", __func__, err);
279
280         return err;
281 }
282
283 static int max9635_enable(struct max9635_data *als_data)
284 {
285         int err;
286
287         if (!atomic_cmpxchg(&als_data->enabled, 0, 1)) {
288                 if (!IS_ERR_OR_NULL(als_data->regulator))
289                         regulator_enable(als_data->regulator);
290                 err = max9635_device_power(als_data, 0x01);
291                 if (err) {
292                         atomic_set(&als_data->enabled, 0);
293                         return err;
294                 }
295         }
296         return 0;
297 }
298
299 static int max9635_disable(struct max9635_data *als_data)
300 {
301         if (atomic_cmpxchg(&als_data->enabled, 1, 0)) {
302                 if (!IS_ERR_OR_NULL(als_data->regulator))
303                         regulator_disable(als_data->regulator);
304                 max9635_device_power(als_data, 0x00);
305         }
306         cancel_delayed_work_sync(&als_data->working_queue);
307
308         return 0;
309 }
310
311 static int max9635_misc_open(struct inode *inode, struct file *file)
312 {
313         int err;
314         err = nonseekable_open(inode, file);
315         if (err < 0)
316                 return err;
317
318         file->private_data = max9635_misc_data;
319
320         return 0;
321 }
322
323 static long max9635_misc_ioctl(struct file *file,
324                                unsigned int cmd, unsigned long arg)
325 {
326         void __user *argp = (void __user *)arg;
327         u8 enable;
328         struct max9635_data *als_data = file->private_data;
329
330         switch (cmd) {
331         case MAX9635_IOCTL_SET_ENABLE:
332                 if (copy_from_user(&enable, argp, 1))
333                         return -EFAULT;
334                 if (enable > 1)
335                         return -EINVAL;
336
337                 if (enable != 0)
338                         max9635_enable(als_data);
339                 else
340                         max9635_disable(als_data);
341
342                 break;
343
344         case MAX9635_IOCTL_GET_ENABLE:
345                 enable = atomic_read(&als_data->enabled);
346                 if (copy_to_user(argp, &enable, 1))
347                         return -EINVAL;
348
349                 break;
350
351         default:
352                 return -EINVAL;
353         }
354
355         return 0;
356 }
357
358 static const struct file_operations max9635_misc_fops = {
359         .owner = THIS_MODULE,
360         .open = max9635_misc_open,
361         .unlocked_ioctl = max9635_misc_ioctl,
362 };
363
364 static struct miscdevice max9635_misc_device = {
365         .minor = MISC_DYNAMIC_MINOR,
366         .name = MAX9635_NAME,
367         .fops = &max9635_misc_fops,
368 };
369 #ifdef DEBUG
370 static ssize_t max9635_registers_show(struct device *dev,
371                                       struct device_attribute *attr, char *buf)
372 {
373         struct i2c_client *client = container_of(dev, struct i2c_client,
374                                                  dev);
375         struct max9635_data *als_data = i2c_get_clientdata(client);
376         unsigned i, n, reg_count;
377         u8 als_reg[2];
378
379         reg_count = sizeof(max9635_regs) / sizeof(max9635_regs[0]);
380         for (i = 0, n = 0; i < reg_count; i++) {
381                 als_reg[0] = (AUTO_INCREMENT | max9635_regs[i].reg);
382                 max9635_read_reg(als_data, als_reg, 1);
383                 n += scnprintf(buf + n, PAGE_SIZE - n,
384                                "%-20s = 0x%02X\n",
385                                max9635_regs[i].name, als_reg[0]);
386         }
387
388         return n;
389 }
390
391 static ssize_t max9635_registers_store(struct device *dev,
392                                        struct device_attribute *attr,
393                                        const char *buf, size_t count)
394 {
395         struct i2c_client *client = container_of(dev, struct i2c_client,
396                                                  dev);
397         struct max9635_data *als_data = i2c_get_clientdata(client);
398         unsigned i, reg_count, value;
399         int error;
400         u8 als_reg[2];
401         char name[30];
402
403         if (count >= 30) {
404                 pr_err("%s:input too long\n", __func__);
405                 return -1;
406         }
407
408         if (sscanf(buf, "%s %x", name, &value) != 2) {
409                 pr_err("%s:unable to parse input\n", __func__);
410                 return -1;
411         }
412
413         reg_count = sizeof(max9635_regs) / sizeof(max9635_regs[0]);
414         for (i = 0; i < reg_count; i++) {
415                 if (!strcmp(name, max9635_regs[i].name)) {
416                         als_reg[0] = (AUTO_INCREMENT | max9635_regs[i].reg);
417                         als_reg[1] = value;
418                         error = max9635_write_reg(als_data, als_reg, 1);
419                         if (error) {
420                                 pr_err("%s:Failed to write register %s\n",
421                                        __func__, name);
422                                 return -1;
423                         }
424                         return count;
425                 }
426         }
427         if (!strcmp("Go", name)) {
428                 max9635_enable(als_data);
429                 return 0;
430         }
431         if (!strcmp("Stop", name)) {
432                 max9635_disable(als_data);
433                 return 0;
434         }
435         pr_err("%s:no such register %s\n", __func__, name);
436         return -1;
437 }
438
439 static DEVICE_ATTR(registers, 0644, max9635_registers_show,
440                    max9635_registers_store);
441 #endif
442
443 static void max9635_work_queue(struct work_struct *work)
444 {
445         struct max9635_data *als_data =
446                 container_of((struct delayed_work *)work, struct max9635_data,
447                              working_queue);
448
449         max9635_report_input(als_data);
450 }
451
452 static int max9635_probe(struct i2c_client *client,
453                          const struct i2c_device_id *id)
454 {
455         struct max9635_platform_data *pdata = client->dev.platform_data;
456         struct max9635_data *als_data;
457         int error = 0;
458
459         if (pdata == NULL) {
460                 pr_err("%s: platform data required\n", __func__);
461                 return -ENODEV;
462         } else if (!client->irq) {
463                 pr_err("%s: polling mode currently not supported\n", __func__);
464                 return -ENODEV;
465         }
466         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
467                 pr_err("%s:I2C_FUNC_I2C not supported\n", __func__);
468                 return -ENODEV;
469         }
470
471         als_data = kzalloc(sizeof(struct max9635_data), GFP_KERNEL);
472         if (als_data == NULL) {
473                 error = -ENOMEM;
474                 goto err_alloc_data_failed;
475         }
476
477         als_data->client = client;
478         als_data->als_pdata = pdata;
479         max9635_misc_data = als_data;
480
481         als_data->idev = input_allocate_device();
482         if (!als_data->idev) {
483                 error = -ENOMEM;
484                 pr_err("%s: input device allocate failed: %d\n", __func__,
485                        error);
486                 goto error_input_allocate_failed;
487         }
488
489         als_data->idev->name = "max9635_als";
490         input_set_capability(als_data->idev, EV_MSC, MSC_RAW);
491
492         error = misc_register(&max9635_misc_device);
493         if (error < 0) {
494                 pr_err("%s: max9635 register failed\n", __func__);
495                 goto error_misc_register_failed;
496         }
497
498         atomic_set(&als_data->enabled, 0);
499
500         INIT_DELAYED_WORK(&als_data->working_queue, max9635_work_queue);
501
502         error = input_register_device(als_data->idev);
503         if (error) {
504                 pr_err("%s: input device register failed:%d\n", __func__,
505                        error);
506                 goto error_input_register_failed;
507         }
508
509         error = max9635_init_registers(als_data);
510         if (error < 0) {
511                 pr_err("%s: Register Initialization failed: %d\n",
512                        __func__, error);
513                 error = -ENODEV;
514                 goto err_reg_init_failed;
515         }
516
517         spin_lock_init(&als_data->irq_lock);
518         als_data->cur_irq_state = 1;
519
520         error = request_irq(als_data->client->irq, max9635_irq_handler,
521                             IRQF_TRIGGER_FALLING, MAX9635_NAME, als_data);
522         if (error != 0) {
523                 pr_err("%s: irq request failed: %d\n", __func__, error);
524                 error = -ENODEV;
525                 goto err_req_irq_failed;
526         }
527
528         i2c_set_clientdata(client, als_data);
529
530         als_data->regulator = regulator_get(&client->dev, "vio");
531
532 #ifdef DEBUG
533         error = device_create_file(&als_data->client->dev, &dev_attr_registers);
534         if (error < 0) {
535                 pr_err("%s:File device creation failed: %d\n", __func__, error);
536                 error = -ENODEV;
537                 goto err_create_registers_file_failed;
538         }
539 #endif
540
541         return 0;
542
543 #ifdef DEBUG
544 err_create_registers_file_failed:
545         free_irq(als_data->client->irq, als_data);
546 #endif
547 err_req_irq_failed:
548 err_reg_init_failed:
549         input_unregister_device(als_data->idev);
550 error_input_register_failed:
551         misc_deregister(&max9635_misc_device);
552 error_misc_register_failed:
553         input_free_device(als_data->idev);
554 error_input_allocate_failed:
555         kfree(als_data);
556 err_alloc_data_failed:
557         return error;
558 }
559
560 static int max9635_remove(struct i2c_client *client)
561 {
562         struct max9635_data *als_data = i2c_get_clientdata(client);
563 #ifdef DEBUG
564         device_remove_file(&als_data->client->dev, &dev_attr_registers);
565 #endif
566         if (!IS_ERR_OR_NULL(als_data->regulator))
567                 regulator_put(als_data->regulator);
568         free_irq(als_data->client->irq, als_data);
569         input_unregister_device(als_data->idev);
570         input_free_device(als_data->idev);
571         misc_deregister(&max9635_misc_device);
572         kfree(als_data);
573         return 0;
574 }
575
576 static int max9635_suspend(struct i2c_client *client, pm_message_t mesg)
577 {
578         struct max9635_data *als_data = i2c_get_clientdata(client);
579
580         if (max9635_debug)
581                 pr_info("%s: Suspending\n", __func__);
582
583         max9635_irq_enable(als_data, 0);
584         cancel_delayed_work_sync(&als_data->working_queue);
585
586         return 0;
587 }
588
589 static int max9635_resume(struct i2c_client *client)
590 {
591         struct max9635_data *als_data = i2c_get_clientdata(client);
592
593         if (max9635_debug)
594                 pr_info("%s: Resuming\n", __func__);
595
596         max9635_irq_enable(als_data, 1);
597
598         return 0;
599 }
600
601 static const struct i2c_device_id max9635_id[] = {
602         {MAX9635_NAME, 0},
603         {}
604 };
605
606 static struct i2c_driver max9635_i2c_driver = {
607         .probe = max9635_probe,
608         .remove = max9635_remove,
609         .suspend = max9635_suspend,
610         .resume = max9635_resume,
611         .id_table = max9635_id,
612         .driver = {
613            .name = MAX9635_NAME,
614            .owner = THIS_MODULE,
615         },
616 };
617
618 static int __init max9635_init(void)
619 {
620         return i2c_add_driver(&max9635_i2c_driver);
621 }
622
623 static void __exit max9635_exit(void)
624 {
625         i2c_del_driver(&max9635_i2c_driver);
626 }
627
628 module_init(max9635_init);
629 module_exit(max9635_exit);
630
631 MODULE_DESCRIPTION("ALS driver for Maxim 9635");
632 MODULE_AUTHOR("Dan Murphy <D.Murphy@motorola.com>");
633 MODULE_LICENSE("GPL");