2 * Copyright (C) 2010 Motorola, Inc.
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.
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.
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
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>
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
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
52 struct max9635_zone_conv {
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];
66 struct regulator *regulator;
69 struct max9635_data *max9635_misc_data;
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},
87 static uint32_t max9635_debug = 0x00;
88 module_param_named(als_debug, max9635_debug, uint, 0664);
90 static int max9635_read_reg(struct max9635_data *als_data, u8 * buf, int len)
94 struct i2c_msg msgs[] = {
96 .addr = als_data->client->addr,
97 .flags = als_data->client->flags & I2C_M_TEN,
102 .addr = als_data->client->addr,
103 .flags = (als_data->client->flags & I2C_M_TEN) | I2C_M_RD,
110 err = i2c_transfer(als_data->client->adapter, msgs, 2);
112 msleep_interruptible(MAX9635_I2C_RETRY_DELAY);
113 } while ((err != 2) && (++tries < MAX9635_MAX_RW_RETRIES));
116 pr_err("%s:read transfer error\n", __func__);
125 static int max9635_write_reg(struct max9635_data *als_data, u8 * buf, int len)
129 struct i2c_msg msgs[] = {
131 .addr = als_data->client->addr,
132 .flags = als_data->client->flags & I2C_M_TEN,
139 err = i2c_transfer(als_data->client->adapter, msgs, 1);
141 msleep_interruptible(MAX9635_I2C_RETRY_DELAY);
142 } while ((err != 1) && (++tries < MAX9635_MAX_RW_RETRIES));
145 pr_err("%s:write transfer error\n", __func__);
154 static int max9635_init_registers(struct max9635_data *als_data)
158 buf[0] = (AUTO_INCREMENT | MAX9635_CONFIGURE);
159 buf[1] = als_data->als_pdata->configure;
160 if (max9635_write_reg(als_data, buf, 1))
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))
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))
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))
181 pr_err("%s:Register 0x%d initialization failed\n", __func__, buf[0]);
185 static void max9635_irq_enable(struct max9635_data *als_data, int enable)
189 spin_lock_irqsave(&als_data->irq_lock, flags);
190 if (als_data->cur_irq_state != enable) {
192 enable_irq(als_data->client->irq);
194 disable_irq_nosync(als_data->client->irq);
195 als_data->cur_irq_state = enable;
197 spin_unlock_irqrestore(&als_data->irq_lock, flags);
200 static irqreturn_t max9635_irq_handler(int irq, void *dev)
202 struct max9635_data *als_data = dev;
204 max9635_irq_enable(als_data, 0);
205 schedule_delayed_work(&als_data->working_queue, 0);
210 static int max9635_read_adj_als(struct max9635_data *als_data)
214 u8 low_buf = MAX9635_ALS_DATA_L;
215 u8 high_buf = MAX9635_ALS_DATA_H;
219 ret = max9635_read_reg(als_data, &high_buf, 1);
221 pr_err("%s: Unable to read lux high byte register: %d\n",
225 ret = max9635_read_reg(als_data, &low_buf, 1);
227 pr_err("%s: Unable to read lux low byte register: %d\n",
232 exponent = (high_buf & 0xf0) >> 4;
233 mantissa = ((high_buf & 0x0f) << 4) | (low_buf & 0x0f);
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);
240 lux = lux * (exponent + als_data->als_pdata->lens_coeff);
242 if (max9635_debug & 1)
243 pr_info("%s:Reporting LUX %d\n", __func__, lux);
247 static int max9635_report_input(struct max9635_data *als_data)
251 u8 buf[2] = { MAX9635_INT_STATUS, 0x00 };
253 lux_val = max9635_read_adj_als(als_data);
255 input_event(als_data->idev, EV_MSC, MSC_RAW, lux_val);
256 input_sync(als_data->idev);
259 /* Clear the interrupt status register */
260 ret = max9635_read_reg(als_data, buf, 1);
262 pr_err("%s:Unable to read interrupt register: %d\n",
266 max9635_irq_enable(als_data, 1);
270 static int max9635_device_power(struct max9635_data *als_data, u8 state)
273 u8 buf[2] = { (AUTO_INCREMENT | MAX9635_INT_EN) };
276 err = max9635_write_reg(als_data, buf, 1);
278 pr_err("%s:Unable to turn off prox: %d\n", __func__, err);
283 static int max9635_enable(struct max9635_data *als_data)
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);
292 atomic_set(&als_data->enabled, 0);
299 static int max9635_disable(struct max9635_data *als_data)
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);
306 cancel_delayed_work_sync(&als_data->working_queue);
311 static int max9635_misc_open(struct inode *inode, struct file *file)
314 err = nonseekable_open(inode, file);
318 file->private_data = max9635_misc_data;
323 static long max9635_misc_ioctl(struct file *file,
324 unsigned int cmd, unsigned long arg)
326 void __user *argp = (void __user *)arg;
328 struct max9635_data *als_data = file->private_data;
331 case MAX9635_IOCTL_SET_ENABLE:
332 if (copy_from_user(&enable, argp, 1))
338 max9635_enable(als_data);
340 max9635_disable(als_data);
344 case MAX9635_IOCTL_GET_ENABLE:
345 enable = atomic_read(&als_data->enabled);
346 if (copy_to_user(argp, &enable, 1))
358 static const struct file_operations max9635_misc_fops = {
359 .owner = THIS_MODULE,
360 .open = max9635_misc_open,
361 .unlocked_ioctl = max9635_misc_ioctl,
364 static struct miscdevice max9635_misc_device = {
365 .minor = MISC_DYNAMIC_MINOR,
366 .name = MAX9635_NAME,
367 .fops = &max9635_misc_fops,
370 static ssize_t max9635_registers_show(struct device *dev,
371 struct device_attribute *attr, char *buf)
373 struct i2c_client *client = container_of(dev, struct i2c_client,
375 struct max9635_data *als_data = i2c_get_clientdata(client);
376 unsigned i, n, reg_count;
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,
385 max9635_regs[i].name, als_reg[0]);
391 static ssize_t max9635_registers_store(struct device *dev,
392 struct device_attribute *attr,
393 const char *buf, size_t count)
395 struct i2c_client *client = container_of(dev, struct i2c_client,
397 struct max9635_data *als_data = i2c_get_clientdata(client);
398 unsigned i, reg_count, value;
404 pr_err("%s:input too long\n", __func__);
408 if (sscanf(buf, "%s %x", name, &value) != 2) {
409 pr_err("%s:unable to parse input\n", __func__);
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);
418 error = max9635_write_reg(als_data, als_reg, 1);
420 pr_err("%s:Failed to write register %s\n",
427 if (!strcmp("Go", name)) {
428 max9635_enable(als_data);
431 if (!strcmp("Stop", name)) {
432 max9635_disable(als_data);
435 pr_err("%s:no such register %s\n", __func__, name);
439 static DEVICE_ATTR(registers, 0644, max9635_registers_show,
440 max9635_registers_store);
443 static void max9635_work_queue(struct work_struct *work)
445 struct max9635_data *als_data =
446 container_of((struct delayed_work *)work, struct max9635_data,
449 max9635_report_input(als_data);
452 static int max9635_probe(struct i2c_client *client,
453 const struct i2c_device_id *id)
455 struct max9635_platform_data *pdata = client->dev.platform_data;
456 struct max9635_data *als_data;
460 pr_err("%s: platform data required\n", __func__);
462 } else if (!client->irq) {
463 pr_err("%s: polling mode currently not supported\n", __func__);
466 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
467 pr_err("%s:I2C_FUNC_I2C not supported\n", __func__);
471 als_data = kzalloc(sizeof(struct max9635_data), GFP_KERNEL);
472 if (als_data == NULL) {
474 goto err_alloc_data_failed;
477 als_data->client = client;
478 als_data->als_pdata = pdata;
479 max9635_misc_data = als_data;
481 als_data->idev = input_allocate_device();
482 if (!als_data->idev) {
484 pr_err("%s: input device allocate failed: %d\n", __func__,
486 goto error_input_allocate_failed;
489 als_data->idev->name = "max9635_als";
490 input_set_capability(als_data->idev, EV_MSC, MSC_RAW);
492 error = misc_register(&max9635_misc_device);
494 pr_err("%s: max9635 register failed\n", __func__);
495 goto error_misc_register_failed;
498 atomic_set(&als_data->enabled, 0);
500 INIT_DELAYED_WORK(&als_data->working_queue, max9635_work_queue);
502 error = input_register_device(als_data->idev);
504 pr_err("%s: input device register failed:%d\n", __func__,
506 goto error_input_register_failed;
509 error = max9635_init_registers(als_data);
511 pr_err("%s: Register Initialization failed: %d\n",
514 goto err_reg_init_failed;
517 spin_lock_init(&als_data->irq_lock);
518 als_data->cur_irq_state = 1;
520 error = request_irq(als_data->client->irq, max9635_irq_handler,
521 IRQF_TRIGGER_FALLING, MAX9635_NAME, als_data);
523 pr_err("%s: irq request failed: %d\n", __func__, error);
525 goto err_req_irq_failed;
528 i2c_set_clientdata(client, als_data);
530 als_data->regulator = regulator_get(&client->dev, "vio");
533 error = device_create_file(&als_data->client->dev, &dev_attr_registers);
535 pr_err("%s:File device creation failed: %d\n", __func__, error);
537 goto err_create_registers_file_failed;
544 err_create_registers_file_failed:
545 free_irq(als_data->client->irq, als_data);
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:
556 err_alloc_data_failed:
560 static int max9635_remove(struct i2c_client *client)
562 struct max9635_data *als_data = i2c_get_clientdata(client);
564 device_remove_file(&als_data->client->dev, &dev_attr_registers);
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);
576 static int max9635_suspend(struct i2c_client *client, pm_message_t mesg)
578 struct max9635_data *als_data = i2c_get_clientdata(client);
581 pr_info("%s: Suspending\n", __func__);
583 max9635_irq_enable(als_data, 0);
584 cancel_delayed_work_sync(&als_data->working_queue);
589 static int max9635_resume(struct i2c_client *client)
591 struct max9635_data *als_data = i2c_get_clientdata(client);
594 pr_info("%s: Resuming\n", __func__);
596 max9635_irq_enable(als_data, 1);
601 static const struct i2c_device_id max9635_id[] = {
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,
613 .name = MAX9635_NAME,
614 .owner = THIS_MODULE,
618 static int __init max9635_init(void)
620 return i2c_add_driver(&max9635_i2c_driver);
623 static void __exit max9635_exit(void)
625 i2c_del_driver(&max9635_i2c_driver);
628 module_init(max9635_init);
629 module_exit(max9635_exit);
631 MODULE_DESCRIPTION("ALS driver for Maxim 9635");
632 MODULE_AUTHOR("Dan Murphy <D.Murphy@motorola.com>");
633 MODULE_LICENSE("GPL");