1*0bf25a45SWolfram Sang /* 2*0bf25a45SWolfram Sang * LM8333 keypad driver 3*0bf25a45SWolfram Sang * Copyright (C) 2012 Wolfram Sang, Pengutronix <w.sang@pengutronix.de> 4*0bf25a45SWolfram Sang * 5*0bf25a45SWolfram Sang * This program is free software; you can redistribute it and/or modify 6*0bf25a45SWolfram Sang * it under the terms of the GNU General Public License as published by 7*0bf25a45SWolfram Sang * the Free Software Foundation; either version 2 of the License. 8*0bf25a45SWolfram Sang */ 9*0bf25a45SWolfram Sang 10*0bf25a45SWolfram Sang #include <linux/module.h> 11*0bf25a45SWolfram Sang #include <linux/slab.h> 12*0bf25a45SWolfram Sang #include <linux/irq.h> 13*0bf25a45SWolfram Sang #include <linux/i2c.h> 14*0bf25a45SWolfram Sang #include <linux/interrupt.h> 15*0bf25a45SWolfram Sang #include <linux/input/matrix_keypad.h> 16*0bf25a45SWolfram Sang #include <linux/input/lm8333.h> 17*0bf25a45SWolfram Sang 18*0bf25a45SWolfram Sang #define LM8333_FIFO_READ 0x20 19*0bf25a45SWolfram Sang #define LM8333_DEBOUNCE 0x22 20*0bf25a45SWolfram Sang #define LM8333_READ_INT 0xD0 21*0bf25a45SWolfram Sang #define LM8333_ACTIVE 0xE4 22*0bf25a45SWolfram Sang #define LM8333_READ_ERROR 0xF0 23*0bf25a45SWolfram Sang 24*0bf25a45SWolfram Sang #define LM8333_KEYPAD_IRQ (1 << 0) 25*0bf25a45SWolfram Sang #define LM8333_ERROR_IRQ (1 << 3) 26*0bf25a45SWolfram Sang 27*0bf25a45SWolfram Sang #define LM8333_ERROR_KEYOVR 0x04 28*0bf25a45SWolfram Sang #define LM8333_ERROR_FIFOOVR 0x40 29*0bf25a45SWolfram Sang 30*0bf25a45SWolfram Sang #define LM8333_FIFO_TRANSFER_SIZE 16 31*0bf25a45SWolfram Sang 32*0bf25a45SWolfram Sang #define LM8333_ROW_SHIFT 4 33*0bf25a45SWolfram Sang #define LM8333_NUM_ROWS 8 34*0bf25a45SWolfram Sang 35*0bf25a45SWolfram Sang 36*0bf25a45SWolfram Sang struct lm8333 { 37*0bf25a45SWolfram Sang struct i2c_client *client; 38*0bf25a45SWolfram Sang struct input_dev *input; 39*0bf25a45SWolfram Sang unsigned short keycodes[LM8333_NUM_ROWS << LM8333_ROW_SHIFT]; 40*0bf25a45SWolfram Sang }; 41*0bf25a45SWolfram Sang 42*0bf25a45SWolfram Sang /* The accessors try twice because the first access may be needed for wakeup */ 43*0bf25a45SWolfram Sang #define LM8333_READ_RETRIES 2 44*0bf25a45SWolfram Sang 45*0bf25a45SWolfram Sang int lm8333_read8(struct lm8333 *lm8333, u8 cmd) 46*0bf25a45SWolfram Sang { 47*0bf25a45SWolfram Sang int retries = 0, ret; 48*0bf25a45SWolfram Sang 49*0bf25a45SWolfram Sang do { 50*0bf25a45SWolfram Sang ret = i2c_smbus_read_byte_data(lm8333->client, cmd); 51*0bf25a45SWolfram Sang } while (ret < 0 && retries++ < LM8333_READ_RETRIES); 52*0bf25a45SWolfram Sang 53*0bf25a45SWolfram Sang return ret; 54*0bf25a45SWolfram Sang } 55*0bf25a45SWolfram Sang 56*0bf25a45SWolfram Sang int lm8333_write8(struct lm8333 *lm8333, u8 cmd, u8 val) 57*0bf25a45SWolfram Sang { 58*0bf25a45SWolfram Sang int retries = 0, ret; 59*0bf25a45SWolfram Sang 60*0bf25a45SWolfram Sang do { 61*0bf25a45SWolfram Sang ret = i2c_smbus_write_byte_data(lm8333->client, cmd, val); 62*0bf25a45SWolfram Sang } while (ret < 0 && retries++ < LM8333_READ_RETRIES); 63*0bf25a45SWolfram Sang 64*0bf25a45SWolfram Sang return ret; 65*0bf25a45SWolfram Sang } 66*0bf25a45SWolfram Sang 67*0bf25a45SWolfram Sang int lm8333_read_block(struct lm8333 *lm8333, u8 cmd, u8 len, u8 *buf) 68*0bf25a45SWolfram Sang { 69*0bf25a45SWolfram Sang int retries = 0, ret; 70*0bf25a45SWolfram Sang 71*0bf25a45SWolfram Sang do { 72*0bf25a45SWolfram Sang ret = i2c_smbus_read_i2c_block_data(lm8333->client, 73*0bf25a45SWolfram Sang cmd, len, buf); 74*0bf25a45SWolfram Sang } while (ret < 0 && retries++ < LM8333_READ_RETRIES); 75*0bf25a45SWolfram Sang 76*0bf25a45SWolfram Sang return ret; 77*0bf25a45SWolfram Sang } 78*0bf25a45SWolfram Sang 79*0bf25a45SWolfram Sang static void lm8333_key_handler(struct lm8333 *lm8333) 80*0bf25a45SWolfram Sang { 81*0bf25a45SWolfram Sang struct input_dev *input = lm8333->input; 82*0bf25a45SWolfram Sang u8 keys[LM8333_FIFO_TRANSFER_SIZE]; 83*0bf25a45SWolfram Sang u8 code, pressed; 84*0bf25a45SWolfram Sang int i, ret; 85*0bf25a45SWolfram Sang 86*0bf25a45SWolfram Sang ret = lm8333_read_block(lm8333, LM8333_FIFO_READ, 87*0bf25a45SWolfram Sang LM8333_FIFO_TRANSFER_SIZE, keys); 88*0bf25a45SWolfram Sang if (ret != LM8333_FIFO_TRANSFER_SIZE) { 89*0bf25a45SWolfram Sang dev_err(&lm8333->client->dev, 90*0bf25a45SWolfram Sang "Error %d while reading FIFO\n", ret); 91*0bf25a45SWolfram Sang return; 92*0bf25a45SWolfram Sang } 93*0bf25a45SWolfram Sang 94*0bf25a45SWolfram Sang for (i = 0; keys[i] && i < LM8333_FIFO_TRANSFER_SIZE; i++) { 95*0bf25a45SWolfram Sang pressed = keys[i] & 0x80; 96*0bf25a45SWolfram Sang code = keys[i] & 0x7f; 97*0bf25a45SWolfram Sang 98*0bf25a45SWolfram Sang input_event(input, EV_MSC, MSC_SCAN, code); 99*0bf25a45SWolfram Sang input_report_key(input, lm8333->keycodes[code], pressed); 100*0bf25a45SWolfram Sang } 101*0bf25a45SWolfram Sang 102*0bf25a45SWolfram Sang input_sync(input); 103*0bf25a45SWolfram Sang } 104*0bf25a45SWolfram Sang 105*0bf25a45SWolfram Sang static irqreturn_t lm8333_irq_thread(int irq, void *data) 106*0bf25a45SWolfram Sang { 107*0bf25a45SWolfram Sang struct lm8333 *lm8333 = data; 108*0bf25a45SWolfram Sang u8 status = lm8333_read8(lm8333, LM8333_READ_INT); 109*0bf25a45SWolfram Sang 110*0bf25a45SWolfram Sang if (!status) 111*0bf25a45SWolfram Sang return IRQ_NONE; 112*0bf25a45SWolfram Sang 113*0bf25a45SWolfram Sang if (status & LM8333_ERROR_IRQ) { 114*0bf25a45SWolfram Sang u8 err = lm8333_read8(lm8333, LM8333_READ_ERROR); 115*0bf25a45SWolfram Sang 116*0bf25a45SWolfram Sang if (err & (LM8333_ERROR_KEYOVR | LM8333_ERROR_FIFOOVR)) { 117*0bf25a45SWolfram Sang u8 dummy[LM8333_FIFO_TRANSFER_SIZE]; 118*0bf25a45SWolfram Sang 119*0bf25a45SWolfram Sang lm8333_read_block(lm8333, LM8333_FIFO_READ, 120*0bf25a45SWolfram Sang LM8333_FIFO_TRANSFER_SIZE, dummy); 121*0bf25a45SWolfram Sang } 122*0bf25a45SWolfram Sang dev_err(&lm8333->client->dev, "Got error %02x\n", err); 123*0bf25a45SWolfram Sang } 124*0bf25a45SWolfram Sang 125*0bf25a45SWolfram Sang if (status & LM8333_KEYPAD_IRQ) 126*0bf25a45SWolfram Sang lm8333_key_handler(lm8333); 127*0bf25a45SWolfram Sang 128*0bf25a45SWolfram Sang return IRQ_HANDLED; 129*0bf25a45SWolfram Sang } 130*0bf25a45SWolfram Sang 131*0bf25a45SWolfram Sang static int __devinit lm8333_probe(struct i2c_client *client, 132*0bf25a45SWolfram Sang const struct i2c_device_id *id) 133*0bf25a45SWolfram Sang { 134*0bf25a45SWolfram Sang const struct lm8333_platform_data *pdata = client->dev.platform_data; 135*0bf25a45SWolfram Sang struct lm8333 *lm8333; 136*0bf25a45SWolfram Sang struct input_dev *input; 137*0bf25a45SWolfram Sang int err, active_time; 138*0bf25a45SWolfram Sang 139*0bf25a45SWolfram Sang if (!pdata) 140*0bf25a45SWolfram Sang return -EINVAL; 141*0bf25a45SWolfram Sang 142*0bf25a45SWolfram Sang active_time = pdata->active_time ?: 500; 143*0bf25a45SWolfram Sang if (active_time / 3 <= pdata->debounce_time / 3) { 144*0bf25a45SWolfram Sang dev_err(&client->dev, "Active time not big enough!\n"); 145*0bf25a45SWolfram Sang return -EINVAL; 146*0bf25a45SWolfram Sang } 147*0bf25a45SWolfram Sang 148*0bf25a45SWolfram Sang lm8333 = kzalloc(sizeof(*lm8333), GFP_KERNEL); 149*0bf25a45SWolfram Sang input = input_allocate_device(); 150*0bf25a45SWolfram Sang if (!lm8333 || !input) { 151*0bf25a45SWolfram Sang err = -ENOMEM; 152*0bf25a45SWolfram Sang goto free_mem; 153*0bf25a45SWolfram Sang } 154*0bf25a45SWolfram Sang 155*0bf25a45SWolfram Sang lm8333->client = client; 156*0bf25a45SWolfram Sang lm8333->input = input; 157*0bf25a45SWolfram Sang 158*0bf25a45SWolfram Sang input->name = client->name; 159*0bf25a45SWolfram Sang input->dev.parent = &client->dev; 160*0bf25a45SWolfram Sang input->id.bustype = BUS_I2C; 161*0bf25a45SWolfram Sang 162*0bf25a45SWolfram Sang input->keycode = lm8333->keycodes; 163*0bf25a45SWolfram Sang input->keycodesize = sizeof(lm8333->keycodes[0]); 164*0bf25a45SWolfram Sang input->keycodemax = ARRAY_SIZE(lm8333->keycodes); 165*0bf25a45SWolfram Sang input->evbit[0] = BIT_MASK(EV_KEY); 166*0bf25a45SWolfram Sang input_set_capability(input, EV_MSC, MSC_SCAN); 167*0bf25a45SWolfram Sang 168*0bf25a45SWolfram Sang matrix_keypad_build_keymap(pdata->matrix_data, LM8333_ROW_SHIFT, 169*0bf25a45SWolfram Sang input->keycode, input->keybit); 170*0bf25a45SWolfram Sang 171*0bf25a45SWolfram Sang if (pdata->debounce_time) { 172*0bf25a45SWolfram Sang err = lm8333_write8(lm8333, LM8333_DEBOUNCE, 173*0bf25a45SWolfram Sang pdata->debounce_time / 3); 174*0bf25a45SWolfram Sang if (err) 175*0bf25a45SWolfram Sang dev_warn(&client->dev, "Unable to set debounce time\n"); 176*0bf25a45SWolfram Sang } 177*0bf25a45SWolfram Sang 178*0bf25a45SWolfram Sang if (pdata->active_time) { 179*0bf25a45SWolfram Sang err = lm8333_write8(lm8333, LM8333_ACTIVE, 180*0bf25a45SWolfram Sang pdata->active_time / 3); 181*0bf25a45SWolfram Sang if (err) 182*0bf25a45SWolfram Sang dev_warn(&client->dev, "Unable to set active time\n"); 183*0bf25a45SWolfram Sang } 184*0bf25a45SWolfram Sang 185*0bf25a45SWolfram Sang err = request_threaded_irq(client->irq, NULL, lm8333_irq_thread, 186*0bf25a45SWolfram Sang IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 187*0bf25a45SWolfram Sang "lm8333", lm8333); 188*0bf25a45SWolfram Sang if (err) 189*0bf25a45SWolfram Sang goto free_mem; 190*0bf25a45SWolfram Sang 191*0bf25a45SWolfram Sang err = input_register_device(input); 192*0bf25a45SWolfram Sang if (err) 193*0bf25a45SWolfram Sang goto free_irq; 194*0bf25a45SWolfram Sang 195*0bf25a45SWolfram Sang i2c_set_clientdata(client, lm8333); 196*0bf25a45SWolfram Sang return 0; 197*0bf25a45SWolfram Sang 198*0bf25a45SWolfram Sang free_irq: 199*0bf25a45SWolfram Sang free_irq(client->irq, lm8333); 200*0bf25a45SWolfram Sang free_mem: 201*0bf25a45SWolfram Sang input_free_device(input); 202*0bf25a45SWolfram Sang kfree(lm8333); 203*0bf25a45SWolfram Sang return err; 204*0bf25a45SWolfram Sang } 205*0bf25a45SWolfram Sang 206*0bf25a45SWolfram Sang static int __devexit lm8333_remove(struct i2c_client *client) 207*0bf25a45SWolfram Sang { 208*0bf25a45SWolfram Sang struct lm8333 *lm8333 = i2c_get_clientdata(client); 209*0bf25a45SWolfram Sang 210*0bf25a45SWolfram Sang free_irq(client->irq, lm8333); 211*0bf25a45SWolfram Sang input_unregister_device(lm8333->input); 212*0bf25a45SWolfram Sang kfree(lm8333); 213*0bf25a45SWolfram Sang 214*0bf25a45SWolfram Sang return 0; 215*0bf25a45SWolfram Sang } 216*0bf25a45SWolfram Sang 217*0bf25a45SWolfram Sang static const struct i2c_device_id lm8333_id[] = { 218*0bf25a45SWolfram Sang { "lm8333", 0 }, 219*0bf25a45SWolfram Sang { } 220*0bf25a45SWolfram Sang }; 221*0bf25a45SWolfram Sang MODULE_DEVICE_TABLE(i2c, lm8333_id); 222*0bf25a45SWolfram Sang 223*0bf25a45SWolfram Sang static struct i2c_driver lm8333_driver = { 224*0bf25a45SWolfram Sang .driver = { 225*0bf25a45SWolfram Sang .name = "lm8333", 226*0bf25a45SWolfram Sang .owner = THIS_MODULE, 227*0bf25a45SWolfram Sang }, 228*0bf25a45SWolfram Sang .probe = lm8333_probe, 229*0bf25a45SWolfram Sang .remove = __devexit_p(lm8333_remove), 230*0bf25a45SWolfram Sang .id_table = lm8333_id, 231*0bf25a45SWolfram Sang }; 232*0bf25a45SWolfram Sang module_i2c_driver(lm8333_driver); 233*0bf25a45SWolfram Sang 234*0bf25a45SWolfram Sang MODULE_AUTHOR("Wolfram Sang <w.sang@pengutronix.de>"); 235*0bf25a45SWolfram Sang MODULE_DESCRIPTION("LM8333 keyboard driver"); 236*0bf25a45SWolfram Sang MODULE_LICENSE("GPL v2"); 237