0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include <linux/kernel.h>
0014 #include <linux/module.h>
0015 #include <linux/i2c.h>
0016 #include <linux/input.h>
0017 #include <linux/slab.h>
0018 #include <linux/irq.h>
0019 #include <linux/interrupt.h>
0020 #include <linux/jiffies.h>
0021 #include <linux/delay.h>
0022
0023
0024 #define CHIP_ID 0x00
0025 #define QT1070_CHIP_ID 0x2E
0026
0027 #define FW_VERSION 0x01
0028 #define QT1070_FW_VERSION 0x15
0029
0030 #define DET_STATUS 0x02
0031
0032 #define KEY_STATUS 0x03
0033
0034
0035 #define CALIBRATE_CMD 0x38
0036 #define QT1070_CAL_TIME 200
0037
0038
0039 #define RESET 0x39
0040 #define QT1070_RESET_TIME 255
0041
0042
0043 static const unsigned short qt1070_key2code[] = {
0044 KEY_0, KEY_1, KEY_2, KEY_3,
0045 KEY_4, KEY_5, KEY_6,
0046 };
0047
0048 struct qt1070_data {
0049 struct i2c_client *client;
0050 struct input_dev *input;
0051 unsigned int irq;
0052 unsigned short keycodes[ARRAY_SIZE(qt1070_key2code)];
0053 u8 last_keys;
0054 };
0055
0056 static int qt1070_read(struct i2c_client *client, u8 reg)
0057 {
0058 int ret;
0059
0060 ret = i2c_smbus_read_byte_data(client, reg);
0061 if (ret < 0)
0062 dev_err(&client->dev,
0063 "can not read register, returned %d\n", ret);
0064
0065 return ret;
0066 }
0067
0068 static int qt1070_write(struct i2c_client *client, u8 reg, u8 data)
0069 {
0070 int ret;
0071
0072 ret = i2c_smbus_write_byte_data(client, reg, data);
0073 if (ret < 0)
0074 dev_err(&client->dev,
0075 "can not write register, returned %d\n", ret);
0076
0077 return ret;
0078 }
0079
0080 static bool qt1070_identify(struct i2c_client *client)
0081 {
0082 int id, ver;
0083
0084
0085 id = qt1070_read(client, CHIP_ID);
0086 if (id != QT1070_CHIP_ID) {
0087 dev_err(&client->dev, "ID %d not supported\n", id);
0088 return false;
0089 }
0090
0091
0092 ver = qt1070_read(client, FW_VERSION);
0093 if (ver < 0) {
0094 dev_err(&client->dev, "could not read the firmware version\n");
0095 return false;
0096 }
0097
0098 dev_info(&client->dev, "AT42QT1070 firmware version %x\n", ver);
0099
0100 return true;
0101 }
0102
0103 static irqreturn_t qt1070_interrupt(int irq, void *dev_id)
0104 {
0105 struct qt1070_data *data = dev_id;
0106 struct i2c_client *client = data->client;
0107 struct input_dev *input = data->input;
0108 int i;
0109 u8 new_keys, keyval, mask = 0x01;
0110
0111
0112 qt1070_read(client, DET_STATUS);
0113
0114
0115 new_keys = qt1070_read(client, KEY_STATUS);
0116
0117 for (i = 0; i < ARRAY_SIZE(qt1070_key2code); i++) {
0118 keyval = new_keys & mask;
0119 if ((data->last_keys & mask) != keyval)
0120 input_report_key(input, data->keycodes[i], keyval);
0121 mask <<= 1;
0122 }
0123 input_sync(input);
0124
0125 data->last_keys = new_keys;
0126 return IRQ_HANDLED;
0127 }
0128
0129 static int qt1070_probe(struct i2c_client *client,
0130 const struct i2c_device_id *id)
0131 {
0132 struct qt1070_data *data;
0133 struct input_dev *input;
0134 int i;
0135 int err;
0136
0137 err = i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE);
0138 if (!err) {
0139 dev_err(&client->dev, "%s adapter not supported\n",
0140 dev_driver_string(&client->adapter->dev));
0141 return -ENODEV;
0142 }
0143
0144 if (!client->irq) {
0145 dev_err(&client->dev, "please assign the irq to this device\n");
0146 return -EINVAL;
0147 }
0148
0149
0150 if (!qt1070_identify(client))
0151 return -ENODEV;
0152
0153 data = kzalloc(sizeof(struct qt1070_data), GFP_KERNEL);
0154 input = input_allocate_device();
0155 if (!data || !input) {
0156 dev_err(&client->dev, "insufficient memory\n");
0157 err = -ENOMEM;
0158 goto err_free_mem;
0159 }
0160
0161 data->client = client;
0162 data->input = input;
0163 data->irq = client->irq;
0164
0165 input->name = "AT42QT1070 QTouch Sensor";
0166 input->dev.parent = &client->dev;
0167 input->id.bustype = BUS_I2C;
0168
0169
0170 input->keycode = data->keycodes;
0171 input->keycodesize = sizeof(data->keycodes[0]);
0172 input->keycodemax = ARRAY_SIZE(qt1070_key2code);
0173
0174 __set_bit(EV_KEY, input->evbit);
0175
0176 for (i = 0; i < ARRAY_SIZE(qt1070_key2code); i++) {
0177 data->keycodes[i] = qt1070_key2code[i];
0178 __set_bit(qt1070_key2code[i], input->keybit);
0179 }
0180
0181
0182 qt1070_write(client, CALIBRATE_CMD, 1);
0183 msleep(QT1070_CAL_TIME);
0184
0185
0186 qt1070_write(client, RESET, 1);
0187 msleep(QT1070_RESET_TIME);
0188
0189 err = request_threaded_irq(client->irq, NULL, qt1070_interrupt,
0190 IRQF_TRIGGER_NONE | IRQF_ONESHOT,
0191 client->dev.driver->name, data);
0192 if (err) {
0193 dev_err(&client->dev, "fail to request irq\n");
0194 goto err_free_mem;
0195 }
0196
0197
0198 err = input_register_device(data->input);
0199 if (err) {
0200 dev_err(&client->dev, "Failed to register input device\n");
0201 goto err_free_irq;
0202 }
0203
0204 i2c_set_clientdata(client, data);
0205
0206
0207 qt1070_read(client, DET_STATUS);
0208
0209 return 0;
0210
0211 err_free_irq:
0212 free_irq(client->irq, data);
0213 err_free_mem:
0214 input_free_device(input);
0215 kfree(data);
0216 return err;
0217 }
0218
0219 static int qt1070_remove(struct i2c_client *client)
0220 {
0221 struct qt1070_data *data = i2c_get_clientdata(client);
0222
0223
0224 free_irq(client->irq, data);
0225
0226 input_unregister_device(data->input);
0227 kfree(data);
0228
0229 return 0;
0230 }
0231
0232 #ifdef CONFIG_PM_SLEEP
0233 static int qt1070_suspend(struct device *dev)
0234 {
0235 struct i2c_client *client = to_i2c_client(dev);
0236 struct qt1070_data *data = i2c_get_clientdata(client);
0237
0238 if (device_may_wakeup(dev))
0239 enable_irq_wake(data->irq);
0240
0241 return 0;
0242 }
0243
0244 static int qt1070_resume(struct device *dev)
0245 {
0246 struct i2c_client *client = to_i2c_client(dev);
0247 struct qt1070_data *data = i2c_get_clientdata(client);
0248
0249 if (device_may_wakeup(dev))
0250 disable_irq_wake(data->irq);
0251
0252 return 0;
0253 }
0254 #endif
0255
0256 static SIMPLE_DEV_PM_OPS(qt1070_pm_ops, qt1070_suspend, qt1070_resume);
0257
0258 static const struct i2c_device_id qt1070_id[] = {
0259 { "qt1070", 0 },
0260 { },
0261 };
0262 MODULE_DEVICE_TABLE(i2c, qt1070_id);
0263
0264 #ifdef CONFIG_OF
0265 static const struct of_device_id qt1070_of_match[] = {
0266 { .compatible = "qt1070", },
0267 { },
0268 };
0269 MODULE_DEVICE_TABLE(of, qt1070_of_match);
0270 #endif
0271
0272 static struct i2c_driver qt1070_driver = {
0273 .driver = {
0274 .name = "qt1070",
0275 .of_match_table = of_match_ptr(qt1070_of_match),
0276 .pm = &qt1070_pm_ops,
0277 },
0278 .id_table = qt1070_id,
0279 .probe = qt1070_probe,
0280 .remove = qt1070_remove,
0281 };
0282
0283 module_i2c_driver(qt1070_driver);
0284
0285 MODULE_AUTHOR("Bo Shen <voice.shen@atmel.com>");
0286 MODULE_DESCRIPTION("Driver for AT42QT1070 QTouch sensor");
0287 MODULE_LICENSE("GPL");