0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/i2c.h>
0009 #include <linux/input.h>
0010 #include <linux/interrupt.h>
0011 #include <linux/mfd/max77650.h>
0012 #include <linux/module.h>
0013 #include <linux/platform_device.h>
0014 #include <linux/regmap.h>
0015
0016 #define MAX77650_ONKEY_MODE_MASK BIT(3)
0017 #define MAX77650_ONKEY_MODE_PUSH 0x00
0018 #define MAX77650_ONKEY_MODE_SLIDE BIT(3)
0019
0020 struct max77650_onkey {
0021 struct input_dev *input;
0022 unsigned int code;
0023 };
0024
0025 static irqreturn_t max77650_onkey_falling(int irq, void *data)
0026 {
0027 struct max77650_onkey *onkey = data;
0028
0029 input_report_key(onkey->input, onkey->code, 0);
0030 input_sync(onkey->input);
0031
0032 return IRQ_HANDLED;
0033 }
0034
0035 static irqreturn_t max77650_onkey_rising(int irq, void *data)
0036 {
0037 struct max77650_onkey *onkey = data;
0038
0039 input_report_key(onkey->input, onkey->code, 1);
0040 input_sync(onkey->input);
0041
0042 return IRQ_HANDLED;
0043 }
0044
0045 static int max77650_onkey_probe(struct platform_device *pdev)
0046 {
0047 int irq_r, irq_f, error, mode;
0048 struct max77650_onkey *onkey;
0049 struct device *dev, *parent;
0050 struct regmap *map;
0051 unsigned int type;
0052
0053 dev = &pdev->dev;
0054 parent = dev->parent;
0055
0056 map = dev_get_regmap(parent, NULL);
0057 if (!map)
0058 return -ENODEV;
0059
0060 onkey = devm_kzalloc(dev, sizeof(*onkey), GFP_KERNEL);
0061 if (!onkey)
0062 return -ENOMEM;
0063
0064 error = device_property_read_u32(dev, "linux,code", &onkey->code);
0065 if (error)
0066 onkey->code = KEY_POWER;
0067
0068 if (device_property_read_bool(dev, "maxim,onkey-slide")) {
0069 mode = MAX77650_ONKEY_MODE_SLIDE;
0070 type = EV_SW;
0071 } else {
0072 mode = MAX77650_ONKEY_MODE_PUSH;
0073 type = EV_KEY;
0074 }
0075
0076 error = regmap_update_bits(map, MAX77650_REG_CNFG_GLBL,
0077 MAX77650_ONKEY_MODE_MASK, mode);
0078 if (error)
0079 return error;
0080
0081 irq_f = platform_get_irq_byname(pdev, "nEN_F");
0082 if (irq_f < 0)
0083 return irq_f;
0084
0085 irq_r = platform_get_irq_byname(pdev, "nEN_R");
0086 if (irq_r < 0)
0087 return irq_r;
0088
0089 onkey->input = devm_input_allocate_device(dev);
0090 if (!onkey->input)
0091 return -ENOMEM;
0092
0093 onkey->input->name = "max77650_onkey";
0094 onkey->input->phys = "max77650_onkey/input0";
0095 onkey->input->id.bustype = BUS_I2C;
0096 input_set_capability(onkey->input, type, onkey->code);
0097
0098 error = devm_request_any_context_irq(dev, irq_f, max77650_onkey_falling,
0099 IRQF_ONESHOT, "onkey-down", onkey);
0100 if (error < 0)
0101 return error;
0102
0103 error = devm_request_any_context_irq(dev, irq_r, max77650_onkey_rising,
0104 IRQF_ONESHOT, "onkey-up", onkey);
0105 if (error < 0)
0106 return error;
0107
0108 return input_register_device(onkey->input);
0109 }
0110
0111 static const struct of_device_id max77650_onkey_of_match[] = {
0112 { .compatible = "maxim,max77650-onkey" },
0113 { }
0114 };
0115 MODULE_DEVICE_TABLE(of, max77650_onkey_of_match);
0116
0117 static struct platform_driver max77650_onkey_driver = {
0118 .driver = {
0119 .name = "max77650-onkey",
0120 .of_match_table = max77650_onkey_of_match,
0121 },
0122 .probe = max77650_onkey_probe,
0123 };
0124 module_platform_driver(max77650_onkey_driver);
0125
0126 MODULE_DESCRIPTION("MAXIM 77650/77651 ONKEY driver");
0127 MODULE_AUTHOR("Bartosz Golaszewski <bgolaszewski@baylibre.com>");
0128 MODULE_LICENSE("GPL v2");
0129 MODULE_ALIAS("platform:max77650-onkey");